下面是用gmail smtp发送邮件的
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMail implements Runnable {
private final String smtp_host = "smtp.gmail.com";
private final String from_userName = "xx@gmail.com";
private final String from_passWord = "xx";
private final String show_name = "xxx";
public String email_address;
public String email_subject;
public String email_content;
public SendMail(String email_address, String email_subject, String email_content) {
this.email_address = email_address;
this.email_subject = email_subject;
this.email_content = email_content;
}
public void run() {
executeMailSend(email_address, email_subject, email_content);
}
/**
*
* @param recipients: the mail send to
* @param sendSubject: the mail subject
* @param sendText: the mail content
*/
public void executeMailSend(String recipients, String sendSubject, String sendText) {
try {
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", smtp_host);
props.put("mail.smtp.user", from_userName);
props.put("mail.smtp.password", from_passWord);
props.put("mail.smtp.port", "587"); //gmail smtp port 587
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, null);
session.setDebug(false);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from_userName));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
message.setFrom(new InternetAddress(show_name + "<" + from_userName + ">"));
message.setSubject(sendSubject);
message.setContent(sendText, "text/html;charset=utf-8");
Transport transport = session.getTransport("smtp");
transport.connect(smtp_host, from_userName, from_passWord);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
System.out.println("send an email to " + recipients + " success");
} catch (Exception e) {
e.printStackTrace();
System.out.println("failure! ");
}
}
public static void main (String args[]){
new SendMail("yy@gmail.com","test","hehe").run();
}
}
结果老是报如下的错误
javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. 36sm443504yxh.67
failure!
at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1020)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:716)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:388)
at SendMail.executeMailSend(SendMail.java:59)
at SendMail.run(SendMail.java:25)
at SendMail.main(SendMail.java:70)
折腾了个把小时,苦苦寻求后发现一个解决办法
把上述代码中的
props.put("mail.smtp.port", "587");
替换成
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
究其原因也不太清楚,网上有资料说port 587也可以,不知结果不行
分享到:
相关推荐
赠送jar包:javax.mail-1.5.6.jar; 赠送原API文档:javax.mail-1.5.6-javadoc.jar; 赠送源代码:javax.mail-1.5.6-sources.jar; 赠送Maven依赖信息文件:javax.mail-1.5.6.pom; 包含翻译后的API文档:javax.mail...
javax.mail.MessagingException: Could not connect to SMTP host: zxkaka.com, port: 25; 出现这个问题时,我在foxmail 以及 outlook都可以发送邮件的。 免得自己以后忘记,在源码里注释写的很清楚
赠送jar包:javax.mail-1.6.2.jar; 赠送原API文档:javax.mail-1.6.2-javadoc.jar; 赠送源代码:javax.mail-1.6.2-sources.jar; 赠送Maven依赖信息文件:javax.mail-1.6.2.pom; 包含翻译后的API文档:javax.mail...
ORA-29532: Java call terminated by uncaught Java exception: javax.mail.MessagingException: IOException while sending message; nested exception is: javax.activation.UnsupportedDataTypeException: no ...
import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet....
`javax.mail-1.6.2_javamailjar1.6.2_javamail-1.6.2_javax.mail_java` 这个标题暗示了我们正在讨论的是 JavaMail 的 1.6.2 版本,这是一个稳定且广泛使用的版本。 JavaMail API 提供了一套标准的接口,用于与邮件...
赠送jar包:javax.mail-1.5.6.jar; 赠送原API文档:javax.mail-1.5.6-javadoc.jar; 赠送源代码:javax.mail-1.5.6-sources.jar; 赠送Maven依赖信息文件:javax.mail-1.5.6.pom; 包含翻译后的API文档:javax.mail...
通过错误描述我们发现java.lang.NoClassDefFoundError: javax/mail/MessagingException,主要说找不到这个类。通过查询发现是缺少javax.mail-api-1.6.2.jar和activation-1.1.1.jar包。在maven上下载即可
赠送jar包:javax.servlet-3.0.0.v201112011016.jar; 赠送原API文档:javax.servlet-3.0.0.v201112011016-javadoc.jar; 赠送源代码:javax.servlet-3.0.0.v201112011016-sources.jar; 赠送Maven依赖信息文件:...
在Java编程环境中,图片处理是一项常见的任务,但有时可能会遇到“javax.imageio.IIOException: Unsupported Image Type”的错误。这个异常通常表示Java的`ImageIO`类库无法识别或支持所尝试读取或写入的图像格式。...
javax.mail.AuthenticationFailedException异常处理。
赠送jar包:javax.servlet-3.0.0.v201112011016.jar; 赠送原API文档:javax.servlet-3.0.0.v201112011016-javadoc.jar; 赠送源代码:javax.servlet-3.0.0.v201112011016-sources.jar; 赠送Maven依赖信息文件:...
基于Maven的javax.mail发送邮件例子程序 pom.xml添加依赖jar: <groupId>javax.mail <artifactId>mail ${javamail.version} </dependency> <javamail.version>1.4.1</javamail.version> /* 功能:...
赠送jar包:javax.annotation-api-1.3.2.jar; 赠送原API文档:javax.annotation-api-1.3.2-javadoc.jar; 赠送源代码:javax.annotation-api-1.3.2-sources.jar; 赠送Maven依赖信息文件:javax.annotation-api-...
在Java开发中,`javax.mail`库是一个非常重要的组件,主要用于处理电子邮件的发送与接收。这个库提供了丰富的API,使得开发者能够轻松地实现邮件服务的功能。`javax.mail-1.6.0.jar`是该库的一个特定版本,包含了...
在Java编程环境中,`javax.mail.jar` 是一个非常重要的库,它提供了用于发送和接收电子邮件的API。这个库是JavaMail API的一部分,主要用于处理SMTP(简单邮件传输协议)、POP3(邮局协议版本3)和IMAP(因特网消息...
javax.naming.NamingException: Cannot create resource instance类加载异常,希望可以帮助跟我一样错误的人。
javax.mail.jar-Java发送邮件API。Apache Email基于javax.mail做了一个开源项目,参见:http://commons.apache.org/proper/commons-email/
在给出的示例中,异常堆栈跟踪显示了`nested exception is: class javax.mail.MessagingException: 554 MI:STC 0,smtp10,DsCowLC7LwPACzRN7qcQAA--.2883S2 1295256512`,这是一个特定的SMTP响应代码,表示邮件服务器...
javax.servlet-api-***.jar中文文档.zip,java,javax.servlet-api-***.jar,javax.servlet,javax.servlet-api,***,jar包,Maven,第三方jar包,组件,开源组件,第三方组件,Gradle,javax,servlet,api,中文API文档,手册,开发...