简体   繁体   中英

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. 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." 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.

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. 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();
        }
    }
}

The first,

message.setFrom(new InternetAddress(USERNAME));

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");`

because the library assumes you want to use mail.smtp.auth when you specify a USERNAME and PASSWORD at

Transport.send(message, USERNAME, PASSWORD);

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

Use the specified user name and password to authenticate to the mail server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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