开发学院WEB开发Jsp 使用JavaMail发送邮件 阅读

使用JavaMail发送邮件

 2008-01-05 08:58:52 来源:WEB开发网   
核心提示:/** * Copyright_2006, Liao Xuefeng * Created on 2006-4-7 */package com.crackj2ee.util.mail;import java.util.*;import javax.mail.*;import javax.mail.internet.*;p

/**
 * Copyright_2006, Liao Xuefeng
 * Created on 2006-4-7
 */
package com.crackj2ee.util.mail;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class SimpleMailSender {

   public static void main(String[] arge) throws Exception {
     String from = "abc_abc_abc@163.com";
     String to = "xyz_xyz_xyz@163.com";
     String subject = "Test mail";
     String body = "A text mail";
     PRoperties props = System.getProperties();
     // 设置SMTP邮件服务器:
     props.put("mail.smtp.host", "smtp.163.com");
     // SMTP服务器需要验证:
     props.put("mail.smtp.auth", "true");
     // 传入用户名和口令:
     session session = Session.getDefaultInstance(props,
         new PassWordAuthenticator("your_username", "your_password"));
     // 创建新邮件:
     Message msg = new MimeMessage(session);
     msg.setFrom(new InternetAddress(from));
     msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
     msg.setSubject(subject);
     msg.setText(body);
     msg.setSentDate(new Date());
     // 发送:
     Transport.send(msg);
   }
}

class PasswordAuthenticator extends Authenticator {

   private String username;
   private String password;

   public PasswordAuthenticator(String username, String password) {
     this.username = username;
     this.password = password;
   }

   protected PasswordAuthentication getPasswordAuthentication() {
     return new PasswordAuthentication(username, password);
   }


Tags:使用 JavaMail 发送

编辑录入:爽爽 [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接