Java 必须首先发出STARTTLS命令。-gsmtp;使用gmail帐户发送带有附件的电子邮件
我是编程新手,很抱歉解释得不清楚。我尝试了谷歌上几乎所有可用的答案。但无法找出出现错误的原因。此代码的目的是附加文件并将其发送到电子邮件地址。我没有任何电子邮件服务器Java 必须首先发出STARTTLS命令。-gsmtp;使用gmail帐户发送带有附件的电子邮件,java,email,starttls,Java,Email,Starttls,我是编程新手,很抱歉解释得不清楚。我尝试了谷歌上几乎所有可用的答案。但无法找出出现错误的原因。此代码的目的是附加文件并将其发送到电子邮件地址。我没有任何电子邮件服务器 import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.Me
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class SendEmailMain {
public static void main(String[] args) {
final String username = "myusername@gmail.com";
final String password = "mypassword";
String emailID = "receiversUserName@yahoo.com";
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", true);
props.put("mail.smtp.auth", true);
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(emailID));
message.setSubject("Testing Subject");
message.setText("PFA");
MimeBodyPart messageBodyPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart();
messageBodyPart = new MimeBodyPart();
String file = "snap1.jpg";
String fileName = "attachmentName";
DataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
System.out.println("Sending");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
eclipse控制台中的错误消息
Sending
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. of6sm3574020lbb.11 - gsmtp
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583)
at javax.mail.Transport.send0(Transport.java:169)
at javax.mail.Transport.send(Transport.java:98)
at me.screenful.screenshot.ScreenShotTaker.SendEmailMain.main(SendEmailMain.java:70)
我已经找到了它不起作用的原因。因为我把谷歌的代码复制到了我的项目中。在代码中
props.put("mail.smtp.starttls.enable", true);
props.put("mail.smtp.auth", true);
值true不是用倒逗号表示的。应该是这样的
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
如果出现错误,“530 5.7.0必须首先发出STARTTLS命令。”在运行时。在IDE中: 右键单击项目。单击运行方式,然后单击运行配置 您可以在VM参数框的(X)=参数选项卡中设置参数“-Dmail.smtp.starttls.enable=true” 单击运行按钮 或 在命令提示下: 您正在为java程序提供参数 java-Dmail.smtp.starttls.enable=true.java