Gmail 作为 JavaMail SMTP 服务器
我一直在以 Gmail 作为主机使用 JavaMail API,并且大致了解如何使用它来发送电子邮件.但是有两行代码还是让我很困惑.
I have been working with the JavaMail API with Gmail as my host and have a general understanding of how it can be used to send emails. But there are two lines of code that still confuse me.
message.setFrom(new InternetAddress(USERNAME));
API 说这用于设置此消息中的发件人"属性."但是,当我从代码中删除这一行并发送电子邮件时,电子邮件与该行存在时相比没有明显变化.我认为这对 Gmail 来说是有目的的,以防止垃圾邮件,这让我想知道在使用 Gmail 作为主机时是否有必要这样做.
API says that this is used to "Set the "From" attribute in this Message." But when I delete this line from the code and send the email, the email has no discernible changes from when the line is present. I've assumed this is purposeful on Gmail's part to prevent spam, which leaves me wondering if this is necessary at all when using Gmail as a host.
这也给我带来了麻烦.
props.put("mail.smtp.auth", "true");
根据我收集到的信息,这表明主机是否需要身份验证,Gmail 会这样做.然而,将其设置为 false 似乎什么都不做,并且消息以与设置为 true 时相同的方式和时间发送.为什么会这样?
From what I have gathered, this indicates whether or not the host requires authentication, which Gmail does. Yet setting it to false seems to do nothing and the message is sent in the same manner and time as with it set as true. Why is this the case?
感谢您的帮助.如果有帮助,这是我的所有代码.
Thanks for any help. Here is all my code if it helps.
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import java.util.Properties;
public class SendEmail
{
private String msg;
private String className;
private final String USERNAME = "email@gmail.com";
private final String PASSWORD = "password";
private final String HOST = "smtp.gmail.com";
public SendEmail(String email, String text, String title)
{
String to = email;
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");
Session session = Session.getInstance(props, null);
try
{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(USERNAME));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject(title);
message.setText(text);
Transport.send(message, USERNAME, PASSWORD);
msg = "Email Successfully Sent";
}
catch(Exception ex)
{
msg = ex.getClass().getName();
}
}
}
推荐答案
第一个,
message.setFrom(new InternetAddress(USERNAME));
正在使用 RFC 5321 - 第 3.3 节邮件事务 MAIL
命令(包括 FROM
).同样,mail.smtp.auth
在
is using the RFC 5321 - Section 3.3 Mail Transactions MAIL
command (which includes FROM
). Similarly, the mail.smtp.auth
appears to be optional in
props.put("mail.smtp.auth", "true");`
因为库假定您在指定 USERNAME
和 PASSWORD
时要使用 mail.smtp.auth
because the library assumes you want to use mail.smtp.auth
when you specify a USERNAME
and PASSWORD
at
Transport.send(message, USERNAME, PASSWORD);
Transport.send(Message, String, String)
Javadoc 说(部分)
the Transport.send(Message, String, String)
Javadoc says (in part)
使用指定的用户名和密码对邮件服务器进行身份验证.
Use the specified user name and password to authenticate to the mail server.
相关文章