javaMail

这是我做的一个使用javaMail发送邮件信息的例子、我找了大量的帮助才做出来得
请大家好好看看吧、希望能为你们提供帮助!!!

显示的格式我没有处理、如果看不清楚的话、下面有一个附件、您可以下载看看、内容一样

import java.io.IOException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
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 EmailService {

// 默认主机;邮箱发送;下面的用户名和密码就是你sohu邮箱的username/pwd
private static final String host = "smtp.sohu.com";

// 是谁发的信息
private static final String fromEmail = "nihao919833@sohu.com";

public static void main(String[] args) {
EmailService email = new EmailService();
email.sendMess(null, "libaogang.1990@gmail.com", null, "标题", "内容");
}

/**
* 客户端调用用于发送信息
*
* @param emailToOne:第一个要收信息的邮箱
* @param emailTwo:第二个要收信息的邮箱
* @param emailThree:第三个要收信息的邮箱
* @param subject:标题
* @param body:内容
*/
public void sendMess(String emailToOne, String emailTwo, String emailThree,
String subject, String body) {
send(emailToOne, emailTwo, emailThree, subject, body);
}

private void send(String emailToOne, String emailTwo, String emailThree,
String subject, String body) {

Properties props = null;// 属性类
Session mailsession = null;// 在每一次发送邮件信息是要创建的一个session
Message msg = null;// 用于存储信息类
try {
props = new Properties();
props.put("mail.smtp.host", host);// 所使用的转移邮箱
props.put("mail.smtp.auth", "true");// 必需的
mailsession = Session.getDefaultInstance(props, SmtpAuth.getInstance());
// true:显示运行时信息显示结果;false:不显示执行的结果信息
mailsession.setDebug(false);
/**
* Message.RecipientType.TO
*
* Message.RecipientType.CC
*
* Message.RecipientType.BCC
*/
// 使用每次session创建一个存储信息的类对象、说明此信息只在当前session范围内使用
msg = new MimeMessage(mailsession);
msg.setFrom(new InternetAddress(fromEmail));
if (null != emailToOne || !emailToOne.equals("")) {
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailToOne));
} else if (null != emailTwo || !emailTwo.equals("")) {
msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(emailTwo));
} else if (null != emailThree || !emailThree.equals("")) {
msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(emailThree));
}
msg.setSentDate(new Date());// 发送的时间既当前时间
msg.setSubject(subject);// 相当于标题
msg.setText(body);// 设置内容
msg.saveChanges();// 保存修改项
Transport.send(msg);// 发送信息
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}

/**
* 用于保存输入的用户名/密码
*
* @author Administrator
*
*/
class SmtpAuth extends javax.mail.Authenticator {

private String user;

private String password;

private static SmtpAuth smtpAuth = null;

// 返回当前类的对象;
public static SmtpAuth getInstance() {
if (null == smtpAuth) {
smtpAuth = new SmtpAuth();
}
return smtpAuth;
}

// 使用默认构造函数使用默认制定用户名和密码
public SmtpAuth() {
user = "nihao919833";
password = "**********";// 将*改为你的真正密码
}

public void setUserinfo(String getuser, String getpassword) {
user = getuser;
password = getpassword;
}

protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication(user, password);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值