Gmail作为JavaMail SMTP服务器 [英] Gmail as JavaMail SMTP server

查看:187
本文介绍了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表示这用于设置此消息中的From属性。但是,当我从代码中删除这一行并发送电子邮件时,该电子邮件与该行的出现时没有明显的变化。我认为这是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 / code>在

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.

这篇关于Gmail作为JavaMail SMTP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆