首页 > 学院 > 开发设计 > 正文

发送邮件

2019-11-10 20:29:54
字体:
来源:转载
供稿:网友
package sendmailtest;import java.util.PRoperties;import javax.mail.Authenticator;import javax.mail.Message;import javax.mail.session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;public class SendMail { public static void main(String[] args) { // 收件人电子邮箱 String to="15938702026@163.com"; // 发件人电子邮箱 String from="ytfunny@126.com"; // 指定发送邮件的主机为 localhost String host="smtp.126.com"; // 获取系统属性 Properties pro=System.getProperties(); // 设置邮件服务器 pro.setProperty("mail.smtp.host",host); pro.put("mail.smtp.auth", true); // 获取默认的 Session 对象。 Session session=Session.getDefaultInstance(pro,new Authenticator() { public javax.mail.PassWordAuthentication getPasswordAuthentication(){ //发件人邮件用户名、密码 return new javax.mail.PasswordAuthentication("ytfunny@126.com", "邮箱密码"); } }); // 创建默认的 MimeMessage 对象。 try { MimeMessage message=new MimeMessage(session); // Set From: 头部头字段 message.setFrom(new InternetAddress(from)); // Set To: 头部头字段 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set Subject: 头部头字段 message.setSubject("百炼江湖人未尽!"); // 设置消息体 message.setText("枯骨如山杯莫停!"); // 发送消息 Transport.send(message); System.out.println("发送成功"); } catch (Exception e) { } }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表