【java实现邮件的发送分享】
2013-08-03 14:37
274 查看
java实现邮件的发送ghost win7系统下载分享方法如下:
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);
}
}
}
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发送Internet邮件了!!!分享一下
- java实现邮件的发送分享
- java带图片的邮件发送方法实现
- Phome开发历程分享——JavaMail实现后台邮件发送
- java实现邮件定时发送
- 用java实现的邮件系统四 (javamail发送邮件)
- 在java程序中实现发送邮件的功能
- java发送邮件附件.网页.文本类型的实现(Content的内容设计)
- java免费发送邮件实现
- Java发送邮件找回密码功能的实现
- java免费发送邮件实现
- java 实现注册时发送激活邮件+激活
- java邮件发送的实现
- java实现邮件的发送包括网页文件的发送
- JAVA邮件发送的简单实现
- java邮件发送的简单实现,使用javamail通过smtp协议发信
- Java远程调用邮件服务器,实现邮件发送
- android使用javaemail实现发送邮件
- [置顶] JAVA实现发送邮件(图片和附件)