您的位置:首页 > 编程语言 > Java开发

javaMail实现以html格式发送邮件

2009-07-13 15:46 573 查看
package mail;

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class PageDemo {
 public static void main(String[] args) {
  try {
   Properties props = new Properties();
   props.put("mail.smtp.host", "smtp.sina.com");
   props.put("mail.smtp.auth", "true");
   props.put("mail.bebug", "true");

   // URLName urlName=new URLName(server);
   // PasswordAuthentication pa=new
   // PasswordAuthentication(user,password);
   Session sendMailSession = Session.getDefaultInstance(props, null);
   sendMailSession.setDebug(true);

   // sendMailSession.setPasswordAuthentication (urlName,pa);
   // SMTPTransport transport=new SMTPTransport(sendMailSession,null);
   Transport transport = sendMailSession.getTransport("smtp");
   Message msg = new MimeMessage(sendMailSession);
   msg.setFrom(new InternetAddress("kingsever@sina.com"));
   InternetAddress[] address = { new InternetAddress(
     "wtc455615369@126.com") };
   msg.setRecipients(Message.RecipientType.TO, address);
   msg.setSubject("您好,请审批");
   // msg.setHeader("X-Mailer", "msgsend");
   msg.setSentDate(new Date());
   Multipart mp = new MimeMultipart();
   MimeBodyPart mbp = new MimeBodyPart();

   // 设定邮件内容的类型为 text/plain 或 text/html
   mbp
     .setContent(
       "<a href='http://test.com/sdfj?sfsfjeijflskjdflsjdf' target='blank'>审批</a>",
       "text/html;charset=GB2312");
   mp.addBodyPart(mbp);
   msg.setContent(mp);
   // msg.setText ("<a href='fsdf'>asfsaf</a>");
   transport.connect("smtp.sina.com", "kingsever", "6557043");
   // transport.connect ();
   transport.sendMessage(msg, msg
     .getRecipients(Message.RecipientType.TO));
  } catch (Exception ex) {
   ex.printStackTrace();
  }
 }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息