使用Java发送邮件
2017-07-07 13:40
288 查看
//Java Web发送邮件
try {
Properties prop = new Properties();
//在属性中设置发送邮件服务器地址与协议
prop.setProperty("mail.host", "smtp.126.com"); //邮件服务器地址
prop.setProperty("mail.transport.protocol", "smtp"); //使用的协议
prop.setProperty("mail.stmp.auth", "true"); //是否需要身份验证
//使用JavaMail发送邮件的5个步骤
//1. 创建session
javax.mail.Session session = javax.mail.Session.getInstance(prop);
//开启session的debug模式,这样就可以查看到程序发送Email的运行状态
session.setDebug(true);
//2. 通过session得到Transport对象
Transport ts = session.getTransport();
/*3. 使用邮箱的用户名和密码链接邮件服务器时,发件人需要提交邮箱的用户名
和密码给SMTP服务器
*/
ts.connect("smtp.126.com", 发件人邮箱, 发件人邮箱密码);
//4. 创建邮件
//① 创建邮件对象
Message msg = new MimeMessage(session);
//② 指明邮件发件人
msg.setFrom(new InternetAddress(发件人邮箱));
//③ 指明收件人邮箱
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(收件人邮箱));
//④ 邮件主题
msg.setSubject(邮件主题);
//⑤ 邮件内容
mes.setContent(邮件内容, "text/html;charset=UTF-8");
//5. 发送邮件
ts.sendMessage(msg, msg.getAllRecipients());
ts.close();
} catch (Exception ex) {
}
package com.hcw.javamail;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class JavaMailDemo1 {
public static void main(String[] args) throws MessagingException {
//创建session对象
Properties props = new Properties();
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.transport.protocol", "smtp");//没写的时候 javax.mail.NoSuchProviderException: Invalid protocol: null
Session session = Session.getInstance(props);
session.setDebug(true);
//创建message对象
Message msg = new MimeMessage(session);
msg.setText("你好吗?");
msg.setFrom(new InternetAddress("zhangsan@163.com"));
Transport transport = session.getTransport();
transport.connect("smtp.163.com",25, "zhangsan", "123456");
transport.sendMessage(msg,new Address[]{new InternetAddress("lisi@sina.com")});
//transport.send(msg,new Address[]{new InternetAddress("lisi@sina.com")});
transport.close();
}
}
try {
Properties prop = new Properties();
//在属性中设置发送邮件服务器地址与协议
prop.setProperty("mail.host", "smtp.126.com"); //邮件服务器地址
prop.setProperty("mail.transport.protocol", "smtp"); //使用的协议
prop.setProperty("mail.stmp.auth", "true"); //是否需要身份验证
//使用JavaMail发送邮件的5个步骤
//1. 创建session
javax.mail.Session session = javax.mail.Session.getInstance(prop);
//开启session的debug模式,这样就可以查看到程序发送Email的运行状态
session.setDebug(true);
//2. 通过session得到Transport对象
Transport ts = session.getTransport();
/*3. 使用邮箱的用户名和密码链接邮件服务器时,发件人需要提交邮箱的用户名
和密码给SMTP服务器
*/
ts.connect("smtp.126.com", 发件人邮箱, 发件人邮箱密码);
//4. 创建邮件
//① 创建邮件对象
Message msg = new MimeMessage(session);
//② 指明邮件发件人
msg.setFrom(new InternetAddress(发件人邮箱));
//③ 指明收件人邮箱
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(收件人邮箱));
//④ 邮件主题
msg.setSubject(邮件主题);
//⑤ 邮件内容
mes.setContent(邮件内容, "text/html;charset=UTF-8");
//5. 发送邮件
ts.sendMessage(msg, msg.getAllRecipients());
ts.close();
} catch (Exception ex) {
}
package com.hcw.javamail;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class JavaMailDemo1 {
public static void main(String[] args) throws MessagingException {
//创建session对象
Properties props = new Properties();
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.transport.protocol", "smtp");//没写的时候 javax.mail.NoSuchProviderException: Invalid protocol: null
Session session = Session.getInstance(props);
session.setDebug(true);
//创建message对象
Message msg = new MimeMessage(session);
msg.setText("你好吗?");
msg.setFrom(new InternetAddress("zhangsan@163.com"));
Transport transport = session.getTransport();
transport.connect("smtp.163.com",25, "zhangsan", "123456");
transport.sendMessage(msg,new Address[]{new InternetAddress("lisi@sina.com")});
//transport.send(msg,new Address[]{new InternetAddress("lisi@sina.com")});
transport.close();
}
}
相关文章推荐
- java使用定时器,定时发送邮件
- java 使用javamail发送邮件
- JDK1.5中的线程池(java.util.concurrent.ThreadPoolExecutor)使用简介,线程邮件发送实例
- EBS中使用JAVA方式发送HTML格式邮件
- 使用java发送邮件sp自动发送邮件方法
- 使用java通过smtp完成发送邮件源码
- Java发送邮件在Windows下使用mail发送邮件正常在linux下报错
- java邮件第一篇-使用命令行发送简单邮件
- java项目中添加邮件发送功能(使用apache commons mail开源项目)
- EBS中使用JAVA方式发送HTML格式邮件
- [JAVA100例]047、使用JavaMail发送邮件
- 如何使用Java来调用Notes API发送邮件(包括附件)
- 使用C#和Java发送邮件(转载)
- java邮件第四篇--使用java程序发送简单的文本邮件
- 使用Java发送邮件(javax.mail)
- 使用java通过smtp完成发送邮件源码
- java使用ant部署邮件发送
- java使用代理发送邮件
- 使用C#和Java发送邮件(转载)
- 使用Java编写发送邮件的代码