我编写了发送邮件的java代码,它给出了异常:当我使用端口号465时
com.sun.mail.smtp.SMTPSendFailedException:530-5.5.1需要验证 . 如需了解更多信息,请访问530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 l1sm2119061pbe.54
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
String USERNAME = username;
String PASSWORD = password;
Properties props = new Properties();
props.put("mail.smtp.host", smtpHost);
if(smtpPort != null){
int SMTP_PORT = Integer.parseInt(smtpPort);
props.put("mail.smtp.port", SMTP_PORT);
}
props.put("mail.from",from);
props.put("mail.smtp.starttls.enable", "true");
if( auth == 1){
props.put("mail.smtp.auth", "true");
}
props.put("mail.debug", "true");
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(USERNAME, PASSWORD);
}
});
MimeMessage msg = new MimeMessage(session);
msg.setFrom();
msg.setRecipients(Message.RecipientType.TO, to);
msg.setSubject(subject);
msg.setSentDate(new Date());
Multipart content = new MimeMultipart();
MimeBodyPart bodyPart = new MimeBodyPart();
bodyPart.setText(htmlBody, "UTF-8");
bodyPart.setContent(htmlBody, "text/html");
content.addBodyPart(bodyPart);
if ( attachmentPath != null ){
String[] a = attachmentPath.split(",");
for (int i = 0; i < a.length; i++) {
if( a[i] != null )
{
MimeBodyPart attachmentPart = new MimeBodyPart();
attachmentPart.attachFile(a[i].trim());
content.addBodyPart(attachmentPart);
}
}
}
msg.setContent(content);
Transport.send(msg);
return true
3 回答
代码是自解释的,这是我在项目中使用的工作代码 . 希望这可以帮到你 . 祝好运 .
不足为奇 . -1不是有效的端口号 . 使用正确的端口号时会发生什么?
这也不是标准的SMTP端口 .
使用正确的端口号时会发生什么?
使用以下代码验证并使用
javax.mail
将邮件发送到Gmail服务器 . 处理防火墙和您选择的主机端口 .