java发邮件
2015-08-13 08:54
579 查看
package com;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
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 SendEmail2
{
public static void main(String [] args)
{
// 收件人电子邮箱
String to = "1215402223@qq.com";
String tochao = "zhangweibin@yunxinsoft.cn";
// 发件人电子邮箱
String from = "1215402223@qq.com";
// 指定发送邮件的主机为 localhost
String host = "smtp.qq.com"; //QQ 邮件服务器
// 获取系统属性
Properties properties = System.getProperties();
// 设置邮件服务器
properties.setProperty("mail.smtp.host", host);//
//mail.smtp.host QQ
properties.put("mail.smtp.auth", "true");
// 获取默认session对象
Session session = Session.getDefaultInstance(properties,new Authenticator(){
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("你的邮箱", "邮箱密码"); //发件人邮件用户名、密码
}
});
try{
// 创建默认的 MimeMessage 对象
MimeMessage message = new MimeMessage(session);
// Set From: 头部头字段
message.setFrom(new InternetAddress(from));
// 发件人地址
InternetAddress[] address = { new InternetAddress(to) };
// 发件人地址
InternetAddress[] addresschao = { new InternetAddress(tochao) };
//发送
message.setRecipients(Message.RecipientType.TO, address);
//抄送
message.setRecipients(Message.RecipientType.CC, addresschao);
//定时发送
message.setSentDate(new Date());
// Set Subject: 头字段
message.setSubject("头字段");
// 创建消息部分
BodyPart messageBodyPart = new MimeBodyPart();
// 消息
messageBodyPart.setText("文本内容");
// 创建多重消息
Multipart multipart = new MimeMultipart();
// 设置文本消息部分
multipart.addBodyPart(messageBodyPart);
// 添加附件部分
messageBodyPart = new MimeBodyPart();
String filename = "F:/dataDictionary.json";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
// 发送完整消息
message.setContent(multipart );
// 发送消息
Transport.send(message);
System.out.println("发送成功");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
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 SendEmail2
{
public static void main(String [] args)
{
// 收件人电子邮箱
String to = "1215402223@qq.com";
String tochao = "zhangweibin@yunxinsoft.cn";
// 发件人电子邮箱
String from = "1215402223@qq.com";
// 指定发送邮件的主机为 localhost
String host = "smtp.qq.com"; //QQ 邮件服务器
// 获取系统属性
Properties properties = System.getProperties();
// 设置邮件服务器
properties.setProperty("mail.smtp.host", host);//
//mail.smtp.host QQ
properties.put("mail.smtp.auth", "true");
// 获取默认session对象
Session session = Session.getDefaultInstance(properties,new Authenticator(){
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("你的邮箱", "邮箱密码"); //发件人邮件用户名、密码
}
});
try{
// 创建默认的 MimeMessage 对象
MimeMessage message = new MimeMessage(session);
// Set From: 头部头字段
message.setFrom(new InternetAddress(from));
// 发件人地址
InternetAddress[] address = { new InternetAddress(to) };
// 发件人地址
InternetAddress[] addresschao = { new InternetAddress(tochao) };
//发送
message.setRecipients(Message.RecipientType.TO, address);
//抄送
message.setRecipients(Message.RecipientType.CC, addresschao);
//定时发送
message.setSentDate(new Date());
// Set Subject: 头字段
message.setSubject("头字段");
// 创建消息部分
BodyPart messageBodyPart = new MimeBodyPart();
// 消息
messageBodyPart.setText("文本内容");
// 创建多重消息
Multipart multipart = new MimeMultipart();
// 设置文本消息部分
multipart.addBodyPart(messageBodyPart);
// 添加附件部分
messageBodyPart = new MimeBodyPart();
String filename = "F:/dataDictionary.json";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
// 发送完整消息
message.setContent(multipart );
// 发送消息
Transport.send(message);
System.out.println("发送成功");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
相关文章推荐
- 垃圾邮件过滤器 python简单实现
- C#实现按数据库邮件列表发送邮件的方法
- C#使用自带的email组件发送邮件的方法
- C#.NET采用HTML模板发送电子邮件完整实例
- php简单实现发送带附件的邮件
- C#发送内置图片html格式邮件的方法
- javascript验证邮件地址和MX记录的方法
- C++实现邮件群发的方法
- 用ASP.NET做的个性化的邮件发送系统
- sqlserver数据库使用存储过程和dbmail实现定时发送邮件
- Jmail组件发送邮件之绝对能用的函数
- 我的群发邮件程序
- smtp邮件发送一例
- 用PHP发电子邮件
- JSP发送邮件实例
- 给多个地址发邮件的类
- python发送邮件示例(支持中文邮件标题)
- php 邮件发送问题解决
- php提交表单发送邮件的方法
- python使用smtplib模块通过gmail实现邮件发送的方法