疯狂java 邮箱分享
2013-11-10 17:08
141 查看
又变天了郁闷前些时候还说过.好像是台风过境 ╮(╯▽╰)╭
都没办法出去玩了.
项目里面需要个邮件发送分享功能有些不知道如何实现.
还好老师今天有时间 发的代码如下:
java实现邮件的发送分享方法如下:
public class PopupAuthenticator extends Authenticator{
public PasswordAuthentication getPasswordAuthentication()
{
String username="11111111@qq.com"; //邮箱登录帐号
String pwd = "111111"; //登录密码
return new PasswordAuthentication(username,pwd);
}
}
public class SendMail {
public static void main(String[] args) {
try {
Authenticator auth = new PopupAuthenticator();
Properties mailProps = new Properties();
//邮件信息验证
mailProps.put("mail.smtp.host", "smtp.qq.com");
mailProps.put("mail.smtp.auth", "true");
mailProps.put("username", "111111@qq.com"); //用户名
mailProps.put("password", "111111");//密码
Session mailSession = Session.getDefaultInstance(mailProps, auth);
MimeMessage message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress("22222222@qq.com"));//发件人地址
Address toInternetAddress=new InternetAddress("wwwwww@qq.com");//收件人地址
message.setRecipient(Message.RecipientType.TO, toInternetAddress);
message.setSubject("Mail Test"); //邮件标题
message.setSentDate(new Date()); // 设置邮件发送日期
MimeMultipart multi = new MimeMultipart();
BodyPart textBodyPart = new MimeBodyPart();
//textBodyPart.setText("Hello World!"); //邮件内容
multi.addBodyPart(textBodyPart);
message.setContent(multi);
message.saveChanges();
//下面代码是发送附件
String fileName = "E:/hello.txt"; //发送附件的文件路径
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Hi there is message info "); //邮件内容
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
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 (Exception ex) {
System.err.println("邮件发送失败的原因是:" + ex.getMessage());
System.err.println("具体错误原因:");
ex.printStackTrace(System.err);
}
}
}
写数据库去
都没办法出去玩了.
项目里面需要个邮件发送分享功能有些不知道如何实现.
还好老师今天有时间 发的代码如下:
java实现邮件的发送分享方法如下:
public class PopupAuthenticator extends Authenticator{
public PasswordAuthentication getPasswordAuthentication()
{
String username="11111111@qq.com"; //邮箱登录帐号
String pwd = "111111"; //登录密码
return new PasswordAuthentication(username,pwd);
}
}
public class SendMail {
public static void main(String[] args) {
try {
Authenticator auth = new PopupAuthenticator();
Properties mailProps = new Properties();
//邮件信息验证
mailProps.put("mail.smtp.host", "smtp.qq.com");
mailProps.put("mail.smtp.auth", "true");
mailProps.put("username", "111111@qq.com"); //用户名
mailProps.put("password", "111111");//密码
Session mailSession = Session.getDefaultInstance(mailProps, auth);
MimeMessage message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress("22222222@qq.com"));//发件人地址
Address toInternetAddress=new InternetAddress("wwwwww@qq.com");//收件人地址
message.setRecipient(Message.RecipientType.TO, toInternetAddress);
message.setSubject("Mail Test"); //邮件标题
message.setSentDate(new Date()); // 设置邮件发送日期
MimeMultipart multi = new MimeMultipart();
BodyPart textBodyPart = new MimeBodyPart();
//textBodyPart.setText("Hello World!"); //邮件内容
multi.addBodyPart(textBodyPart);
message.setContent(multi);
message.saveChanges();
//下面代码是发送附件
String fileName = "E:/hello.txt"; //发送附件的文件路径
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Hi there is message info "); //邮件内容
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
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 (Exception ex) {
System.err.println("邮件发送失败的原因是:" + ex.getMessage());
System.err.println("具体错误原因:");
ex.printStackTrace(System.err);
}
}
}
写数据库去
相关文章推荐
- [疯狂Java]泛型:泛型的定义(类、接口、对象)、使用、继承
- 分享下多年积累的对JAVA程序员成长之路的总结
- 500G JAVA视频网盘分享 (Jeecg社区)
- Java APP移动端邮箱认证
- java编写的文件管理器代码分享
- 【纯干货】微信支付接入攻略—JAVA代码分享
- 500 G JAVA视频网盘分享
- 【名师课堂】阿里免费分享Java高级开发
- 怎么上Google学术?怎么上Googl搜索?怎么上Gemail邮箱?外游加速器分享
- 【分享】Java后台开发精选知识图谱
- [疯狂Java]SQL:对查询结果进行集合运算
- [分享] Wex5 作品 仿微信聊天工具 部分代码 详情 留下邮箱
- 微信公众平台开发:JS-SDK之分享功能整理(Java)
- java正则表达式验证手机号码ip,邮箱
- [疯狂Java]JDBC:JDBC/ODBC基本概念、MySQL基本命令、SQL语句基本概念
- [疯狂Java笔记]AWT:CardLayout卡堆布局
- 【分享】Java中如何读写cookie
- Java微信分享接口开发
- Java正则表达式校验邮箱和手机号
- java中HashMap源码学习分享