这是我的课程,其中包含发送电子邮件的方法。我正在使用JavaMail API发送电子邮件。如果我从EmailAccount(< - 它是我创建的类)中传递了正确的参数,我多次检查过,看起来是正确的。问题是我没有收到任何错误或警告,所有发生的事情都是当程序到达Transport.send(msg);
时没有发生任何事情。它不会冻结或崩溃它刚刚运行(就像Transport.send(msg)
永远不会完成)。要关闭程序,我必须从IDE手动中断它。我很乐意解决我的问题。
public class EmailTools
{
public void sendEmail(EmailAccount emailAccount, String addressOfReceiver, String title, String text)
{
Properties props = new Properties();
props.put("mail.smtp.host", emailAccount.getSmtpHost());
props.put("mail.smtp.socketFactory.port", Integer.toString(emailAccount.getSslPort()));
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSlSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", Integer.toString(emailAccount.getSmtpPort()));
props.put("mail.host", emailAccount.getSmtpHost());
//Creating Authenticator
javax.mail.Authenticator auth = new javax.mail.Authenticator()
{
@Override
protected javax.mail.PasswordAuthentication getPasswordAuthentication()
{
return new javax.mail.PasswordAuthentication(emailAccount.getEmailAddress(), emailAccount.getPassword());
}
};
//Starting session
Session session = Session.getDefaultInstance(props, auth);
createAndSendEmail(session, emailAccount, addressOfReceiver, title, text);
}
public void createAndSendEmail(Session session, EmailAccount emailAccount, String receiver, String title, String message)
{
try
{
MimeMessage msg = new MimeMessage(session);
//Header stuff
msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
msg.addHeader("format", "flowed");
msg.addHeader("Content-Transfer-Encoding", "8bit");
msg.setFrom(new InternetAddress(emailAccount.getEmailAddress(), "NoReply-JD"));
msg.setSubject(title, "UTF-8");
msg.setText(message, "UTF-8");
msg.setSentDate(new Date());
msg.setReplyTo(InternetAddress.parse(emailAccount.getEmailAddress(), false));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(receiver, false));
Transport transport = session.getTransport("smtps");
//EDIT1 transport.connect(emailAccount.getSmtpHost(),emailAccount.getSmtpPort(),emailAccount.getEmailAddress(),emailAccount.getPassword());
transport.sendMessage(msg,msg.getAllRecipients());
transport.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
public static void main(String args[])
{
EmailAccount emailAccount = new EmailAccount("example@gmail.com","smtp.gmail.com","password","",465,465);
EmailTools emailTools = new EmailTools();
emailTools.sendEmail(emailAccount,"example@gmail.com","Title","Text");
}
}
EDIT1: 我设法解决了问题,并得到了连接。但现在我得到了例外
javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbuuA
534-5.7.14 2SN9pvdVb-g3yxJj_u5P7eeAJOoLRRJMVevSThdvunt1c2qCcjkt9FRerrmg9YkB3UDbkc
534-5.7.14 wQDcDG8k4c8GcLrteODlY_danNGhcrg_bxE2_SgYioZK4nH0SzNW1K6-ZRCSlm-mTb6Auj
534-5.7.14 UOr5UqyodVDPHCi8fAmsRF-s30sF29nAdzNMjchSccoo3gwHYixRKSTb69XYWpat1SgRHK
534-5.7.14 FIJ5GjQ> Please log in via your web browser and then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 ll20sm5684460wic.14 - gsmtp
EDIT2: 问题解决了。它帮助了我很多gmail问题: Unable to send email via google smtp on centos VPS