- 首先在易邮服务器上配置域名,并且设置两个账号
- 然后到客户端上面将邮件账号设置进去
1.1)给易游服务器设置域名
1.2)创建账号
2.1)客户端添加账号
通过代码发送邮件
package cn.itcast.utils;
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;
import javax.mail.internet.MimeMessage.RecipientType;
public class MailUtils {
//email:邮件发给谁 subject:主题 emailMsg:邮件的内容
public static void sendMail(String email, String subject,String emailMsg)
throws AddressException, MessagingException {
// 1.创建一个程序与邮件服务器会话对象 Session
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "SMTP"); //发邮件的协议
props.setProperty("mail.host", "localhost"); //发邮件的服务器地址
props.setProperty("mail.smtp.auth", "true");// 指定验证为true
// 创建验证器
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("tom", "123456"); //发邮件的账号和密码验证
}
};
Session session = Session.getInstance(props, auth);
// 2.创建一个Message,它相当于是邮件内容
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("tom@guoyebing.cn")); // 设置发送者
message.setRecipient(RecipientType.TO, new InternetAddress(email)); // 设置发送方式与接收者
message.setSubject(subject);
//message.setSubject("用户激活"); 邮件主题
// message.setText("这是一封激活邮件,请<a href='#'>点击</a>");
message.setContent(emailMsg, "text/html;charset=utf-8");
// 3.创建 Transport用于将邮件发送
Transport.send(message);
}
}
调用邮件工具
public class Answer1 {
public static void main(String[] args) throws AddressException, MessagingException {
//email:表示接收者 subject:主题 emailMsg:邮件内容
//MailUtils.sendMail(email,subject emailMsg);
MailUtils.sendMail("lucy@guoyebing.cn","测试邮件","这是一封测试邮件");
}
}