Javamail Api 简明教程
JavaMail API - Gmail SMPT Server
在所有以前的章节中,我们都使用 JangoSMTP 服务器来发送电子邮件。在本章中,我们将了解 Gmail 提供的 SMPT 服务器。Gmail(以及其他)免费提供使用其公共 SMTP 服务器。
In all previous chapters we used JangoSMPT server to send emails. In this chapter we will learn about SMPT server provided by Gmail. Gmail (among others) offers use of their public SMTP server free of charge.
可以在 here 中找到 Gmail SMTP 服务器详细信息。如您在详细信息中所见,我们可以使用 TLS 或 SSL 连接通过 Gmail SMTP 服务器发送电子邮件。
Gmail SMTP server details can be found here. As you can see in the details, we can use either TLS or SSL connection to send email via Gmail SMTP server.
使用 Gmail SMTP 服务器发送电子邮件的过程与第 Sending Emails 章中所述的过程类似,只是我们需要更改主机服务器。作为先决条件,发件人电子邮件地址应为有效的 Gmail 帐户。让我们尝试一个示例。
The procedure to send email using Gmail SMTP server is similar as explained in chapter Sending Emails, except that we would change the host server. As a pre-requisite the sender email address should be an active gmail account. Let us try an example.
Create Java Class
创建一个 Java 文件 SendEmailUsingGMailSMTP ,其内容如下:
Create a Java file SendEmailUsingGMailSMTP, contents of which are as below:
package com.tutorialspoint;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendEmailUsingGMailSMTP {
public static void main(String[] args) {
// Recipient's email ID needs to be mentioned.
String to = "xyz@gmail.com";//change accordingly
// Sender's email ID needs to be mentioned
String from = "abc@gmail.com";//change accordingly
final String username = "abc";//change accordingly
final String password = "*****";//change accordingly
// Assuming you are sending email through relay.jangosmtp.net
String host = "smtp.gmail.com";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");
// Get the Session object.
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
// Create a default MimeMessage object.
Message message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
// Set Subject: header field
message.setSubject("Testing Subject");
// Now set the actual message
message.setText("Hello, this is sample for to check send "
+ "email using JavaMailAPI ");
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
此处,主机已设为 smtp.gmail.com,端口已设为 587。此处,我们已启用 TLS 连接。
Here the host is set as smtp.gmail.com and port is set as 587. Here we have enabled TLS connection.
Compile and Run
现在,我们的类已经准备就绪,我们来编译上述类。我已经将类 SendEmailUsingGMailSMTP.java 保存到目录:@{}。我们需要 classpath 中的 javax.mail.jar 和 activation.jar。执行以下命令从命令提示符编译类(两个 .jar 文件都放在 /home/manisha/ 目录中):
Now that our class is ready, let us compile the above class. I’ve saved the class SendEmailUsingGMailSMTP.java to directory : /home/manisha/JavaMailAPIExercise. We would need the jars javax.mail.jar and activation.jar in the classpath. Execute the command below to compile the class (both the jars are placed in /home/manisha/ directory) from command prompt:
javac -cp /home/manisha/activation.jar:/home/manisha/javax.mail.jar: SendEmailUsingGMailSMTP.java
现在类已编译,执行以下命令来运行:
Now that the class is compiled, execute the below command to run:
java -cp /home/manisha/activation.jar:/home/manisha/javax.mail.jar: SendEmailUsingGMailSMTP