一个简短非常实用的邮件发送
2014-07-23 11:21
323 查看
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MySendMessag2 {
/**
* 邮件发送
*/
public static void send()
{http://www.huiyi8.com/font/ 字体下载
//会话属性设置
Properties properties=new Properties();
//身份验证协议设置
properties.setProperty("mail.smtp.auth", "true");
//发送协议设置
properties.setProperty("mail.transport.protocol", "smtp");
//设置主机服务器
properties.setProperty("mail.host", "smtp.qq.com");
//建立会话机制,注入属性
Session session=Session.getInstance(properties,new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
//邮箱密码设置
return new PasswordAuthentication("用户名", "密码");
//信息的建立
Message message=new MimeMessage(session);
//发送方的邮箱的设置
try {
message.setFrom(new InternetAddress("发送方地址"));
} catch (AddressException e) {
System.out.println("发送方邮箱设置失败");
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//设置主题
try {
message.setSubject("美好的主题");
} catch (MessagingException e1) {
System.out.println("主题设置有误");
e1.printStackTrace();
}
try {//设置发送内容,将格式设置为html或文本格式,编码为UTF-8
message.setContent("<a color='red' href='www.baidu.com'>你</a>","text/html;charset=UTF-8 " );
} catch (MessagingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//接收方邮箱的设置,可以同时有多个接收方,有抄送(cc)和密送(bcc),收件人
try {//按收件人发送给接收方一,接收方二
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(
"接收方一,“接收方2”"));
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//跟踪发送信息
session.setDebug(true);
//发送信息开始
try {
Transport.send(message);
} catch (MessagingException e) {
System.out.println("发送失败");
e.printStackTrace();
* @param args
*/
public static void main(String[] args) {
send();
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MySendMessag2 {
/**
* 邮件发送
*/
public static void send()
{http://www.huiyi8.com/font/ 字体下载
//会话属性设置
Properties properties=new Properties();
//身份验证协议设置
properties.setProperty("mail.smtp.auth", "true");
//发送协议设置
properties.setProperty("mail.transport.protocol", "smtp");
//设置主机服务器
properties.setProperty("mail.host", "smtp.qq.com");
//建立会话机制,注入属性
Session session=Session.getInstance(properties,new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
//邮箱密码设置
return new PasswordAuthentication("用户名", "密码");
//信息的建立
Message message=new MimeMessage(session);
//发送方的邮箱的设置
try {
message.setFrom(new InternetAddress("发送方地址"));
} catch (AddressException e) {
System.out.println("发送方邮箱设置失败");
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//设置主题
try {
message.setSubject("美好的主题");
} catch (MessagingException e1) {
System.out.println("主题设置有误");
e1.printStackTrace();
}
try {//设置发送内容,将格式设置为html或文本格式,编码为UTF-8
message.setContent("<a color='red' href='www.baidu.com'>你</a>","text/html;charset=UTF-8 " );
} catch (MessagingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//接收方邮箱的设置,可以同时有多个接收方,有抄送(cc)和密送(bcc),收件人
try {//按收件人发送给接收方一,接收方二
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(
"接收方一,“接收方2”"));
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//跟踪发送信息
session.setDebug(true);
//发送信息开始
try {
Transport.send(message);
} catch (MessagingException e) {
System.out.println("发送失败");
e.printStackTrace();
* @param args
*/
public static void main(String[] args) {
send();
相关文章推荐
- 一个简短非常实用的邮件发送
- 一个简短非常实用的邮件发送
- 终于用PYTHON作了一个实用于工作的脚本啦----通过邮件定时发送服务器空闲空间
- 一个简单的自动发送邮件系统(一)
- 一个非常实用的javascript读写Cookie函数
- PHP一个发送邮件的类
- php发送邮件的一个类
- 一个非常实用的Base64类(转载)
- 一个非常实用的javascript读写Cookie函数
- 一个简单的自动发送邮件系统(二)
- 一个在线手册(非常实用)
- 一个非常实用的Base64类
- 被一贴,一个以前写的邮件发送的小类库。记住了,不是内裤
- 一个非常实用的ping类
- 一个非常实用的javascript设置cookie函数
- epoll使用的一个小例子,非常实用
- 一个通过web.Mail发送邮件的类
- PHP一个发送邮件的类
- 另外一个ASP.NET 实现邮件发送和接受的功能(Sockets)
- 钓鱼邮件盯上iPhone,钓鱼垃圾邮件又一个发送高潮