简体   繁体   English

Gmail作为JavaMail SMTP服务器

[英]Gmail as JavaMail SMTP server

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. 我一直在使用以Gmail为主机的JavaMail API,并且对如何使用它发送电子邮件有一般的了解。 But there are two lines of code that still confuse me. 但是仍然有两行代码使我感到困惑。

message.setFrom(new InternetAddress(USERNAME));

API says that this is used to "Set the "From" attribute in this Message." API表示,这用于“设置此消息中的“发件人”属性。 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. 我认为这对于防止垃圾邮件在Gmail上是有目的的,这使我想知道在将Gmail用作主机时是否完全有必要这样做。

This is also giving me trouble. 这也给我带来麻烦。

props.put("mail.smtp.auth", "true");

From what I have gathered, this indicates whether or not the host requires authentication, which Gmail does. 根据我收集到的信息,这表明主机是否需要身份验证,Gmail则需要。 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. 但是,将其设置为false似乎无济于事,并且消息的发送时间与设置为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();
        }
    }
}

The first, 首先,

message.setFrom(new InternetAddress(USERNAME));

is using the RFC 5321 - Section 3.3 Mail Transactions MAIL command (which includes FROM ). 正在使用RFC 5321-第3.3节“邮件交易 MAIL命令(包括FROM )。 Similarly, the mail.smtp.auth appears to be optional in 同样, mail.smtp.auth似乎是可选的

props.put("mail.smtp.auth", "true");`

because the library assumes you want to use mail.smtp.auth when you specify a USERNAME and PASSWORD at 因为该库假定您在以下位置指定USERNAMEPASSWORD时要使用mail.smtp.auth

Transport.send(message, USERNAME, PASSWORD);

the Transport.send(Message, String, String) Javadoc says (in part) Transport.send(Message, String, String) Javadoc说(部分)

Use the specified user name and password to authenticate to the mail server. 使用指定的用户名和密码对邮件服务器进行身份验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM