java发邮件
来源:互联网 发布:阿里云os电视软件下载 编辑:程序博客网 时间:2024/06/06 02:42
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();
}
}
}
0 0
- 实战java发邮件
- 实战java发邮件
- 实战java发邮件
- 实战java发邮件
- 实战java发邮件
- Java发邮件
- java发邮件例子
- Java发邮件
- Java发邮件
- JAVA发邮件
- java发邮件
- java 发邮件
- JAVA发邮件
- java发邮件
- java发邮件
- Java发用邮件
- java发邮件
- java发邮件
- 根据mac地址+deviceid获取设备唯一编码 获取手机及SIM卡相关信息
- Java中的内部类(开发中常用,为重点)
- 学习Javascript闭包(Closure)
- C#中一个保持事务原子性的方法,插入数据失败回滚
- android ORM数据库框架ActiveAndroid的简单使用
- java发邮件
- Android读取assets中文件
- 启动Activity的方法
- C - ACboy needs your help again!
- 几种计算机网络体系结构的对比分析
- 学会在职场中成长
- 工作分配问题 解题报告
- vimrc 备份
- WXHL学习总结(五)