使用java代码实现邮件的发送
来源:互联网 发布:linux循环执行命令 编辑:程序博客网 时间:2024/05/08 17:29
package easytest;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
public class A {
public static void main(String[] args) throws UnsupportedEncodingException, MessagingException {
reSendEmail();
}
public static void reSendEmail() throws MessagingException, UnsupportedEncodingException {
String host="smtp.mxhichina.com";//服务器(这是阿狸的可以更换为eg:腾讯qq等)
String password="123456";//邮箱密码
String from="123.com";//邮箱用户名
String email ="1185365895.com";//手机人邮箱地址
//发送邮件;
MimeMessageHelper messageHelper = null;
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", host);
props.setProperty("mail.smtp.auth", "true");
Session session = Session.getInstance(props);
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setDefaultEncoding("UTF-8");
mailSender.setJavaMailProperties(props);
mailSender.setPassword(password);
mailSender.setPort(25);
mailSender.setSession(session);
mailSender.setUsername(from);
MimeMessage createMimeMessage = mailSender.createMimeMessage();
messageHelper = new MimeMessageHelper(createMimeMessage, true, "UTF-8");
messageHelper.setFrom(new InternetAddress(from, "测试邮件", "UTF-8"));
messageHelper.setTo(email);
messageHelper.setSubject("邮件主题");
/*File file = new File(email.getEmailPath());
String fileNm = file.getName();
messageHelper.addAttachment(fileNm, file);*/
messageHelper.setText("邮件内容", true);
mailSender.send(createMimeMessage);
}
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
public class A {
public static void main(String[] args) throws UnsupportedEncodingException, MessagingException {
reSendEmail();
}
public static void reSendEmail() throws MessagingException, UnsupportedEncodingException {
String host="smtp.mxhichina.com";//服务器(这是阿狸的可以更换为eg:腾讯qq等)
String password="123456";//邮箱密码
String from="123.com";//邮箱用户名
String email ="1185365895.com";//手机人邮箱地址
//发送邮件;
MimeMessageHelper messageHelper = null;
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", host);
props.setProperty("mail.smtp.auth", "true");
Session session = Session.getInstance(props);
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setDefaultEncoding("UTF-8");
mailSender.setJavaMailProperties(props);
mailSender.setPassword(password);
mailSender.setPort(25);
mailSender.setSession(session);
mailSender.setUsername(from);
MimeMessage createMimeMessage = mailSender.createMimeMessage();
messageHelper = new MimeMessageHelper(createMimeMessage, true, "UTF-8");
messageHelper.setFrom(new InternetAddress(from, "测试邮件", "UTF-8"));
messageHelper.setTo(email);
messageHelper.setSubject("邮件主题");
/*File file = new File(email.getEmailPath());
String fileNm = file.getName();
messageHelper.addAttachment(fileNm, file);*/
messageHelper.setText("邮件内容", true);
mailSender.send(createMimeMessage);
}
}
如果host;password;form配置不对会报526;535等错
Caused by: javax.mail.AuthenticationFailedException: 526 Authentication failure[0]
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:826)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:761)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:685)
at javax.mail.Service.connect(Service.java:295)
at org.springframework.mail.javamail.JavaMailSenderImpl.connectTransport(JavaMailSenderImpl.java:501)
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:421)
... 4 more
在这里有一些配置:
如何设置IMAP、POP3/SMTP及其SSL加密方式?
如果您的电子邮件客户端支持SSL,可以在设置中选择使用SSL。
通用配置参数:
(我们已经默认都支持这些协议,用户无需自己手动开启这些服务器与端口)
POP3/SMTP协议
接收邮件服务器:pop.exmail.qq.com ,使用SSL,端口号995
发送邮件服务器:smtp.exmail.qq.com ,使用SSL,端口号465
海外用户可使用以下服务器
接收邮件服务器:hwpop.exmail.qq.com ,使用SSL,端口号995
发送邮件服务器:hwsmtp.exmail.qq.com ,使用SSL,端口号465
IMAP协议
接收邮件服务器:imap.exmail.qq.com ,使用SSL,端口号993
发送邮件服务器:smtp.exmail.qq.com ,使用SSL,端口号465
海外用户可使用以下服务器
接收邮件服务器:hwimap.exmail.qq.com ,使用SSL,端口号993
发送邮件服务器:hwsmtp.exmail.qq.com ,使用SSL,端口号465
具体设置可以看:http://service.exmail.qq.com/cgi-bin/help?id=28&no=1000585&subtype=1
阅读全文
0 0
- 使用java代码实现邮件的发送
- java邮件发送的实现代码
- java代码实现邮件发送
- java代码实现发送邮件
- java代码实现发送邮件
- java代码实现发送邮件
- 使用Java实现QQ邮件的发送
- 使用JAVA 代码发送邮件
- java的邮件发送代码
- 用java实现发送邮件的核心代码
- javaMail 的使用,java代码发送一份邮件
- java实现邮件的发送
- java发送邮件的实现
- java邮件发送的实现
- java发送邮件的实现
- JAVA实现邮件的发送
- 使用PHPMailer实现邮件发送代码分享
- java实现简单邮件发送代码
- 腾讯祭出大招VasSonic,让你的H5页面首屏秒开
- 面试题14:调整数组顺序使得奇数位于偶数之前
- Fileter过滤器的使用及小案例
- 计算字符串最后一个单词的长度,单词以空格隔开
- hive基本概念
- 使用java代码实现邮件的发送
- Java程序语言的命名规范
- 前端开发人员应考虑哪些SEO优化技巧
- 支持向量机-手写数字识别
- ListView 设置Item高度
- 使用JDBC进行数据库的事务操作(1)
- ubuntu 16.04 下安装最新版本wineQQ
- 预处理器
- jq中的克隆用法