一个简短非常实用的邮件发送

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();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值