鱼C论坛

 找回密码
 立即注册

java调用163邮箱发送邮件

已有 979 次阅读2013-6-28 14:59 |个人分类:java| java

1:注册一个163邮箱,

  调用发送邮件代码,查询smtp.163.com,作为发送邮件的服务器ip,类似的邮箱服务器应该也可以。[java]

  MailSenderInfo mailInfo = new MailSenderInfo();

  mailInfo.setMailServerHost("121.195.178.51");//邮件服务器ip地址。smtp.163.com,

  mailInfo.setMailServerPort("25");//端口

  mailInfo.setValidate(true);

  mailInfo.setUserName("xiongcheng@126.com");//能够登录126的邮箱

  mailInfo.setPassword("*****");//密码

  mailInfo.setFromAddress("****@163.com");//显示发送发邮箱地址

  mailInfo.setToAddress("****@126.com");//接收邮件地址

  mailInfo.setSubject("标题");

  mailInfo.setContent("发送内容");// 这个类主要来发送邮件

  SimpleMailSender sms = new SimpleMailSender();// 发送html格式

  return sms.sendHtmlMail(mailInfo);

  MailSenderInfo mailInfo = new MailSenderInfo();

  mailInfo.setMailServerHost("121.195.178.51");//邮件服务器ip地址。smtp.163.com,

  mailInfo.setMailServerPort("25");//端口

  mailInfo.setValidate(true);

  mailInfo.setUserName("xiongcheng@126.com");//能够登录126的邮箱

  mailInfo.setPassword("*****");//密码

  mailInfo.setFromAddress("****@163.com");//显示发送发邮箱地址

  mailInfo.setToAddress("****@126.com");//接收邮件地址

  mailInfo.setSubject("标题");

  mailInfo.setContent("发送内容");// 这个类主要来发送邮件

  SimpleMailSender sms = new SimpleMailSender();// 发送html格式

  return sms.sendHtmlMail(mailInfo);邮件发送方法[java] view plaincopyprint?

  /**

  * 以HTML格式发送邮件

  *

  * @param mailInfo

  *            待发送的邮件信息

  */

  public static boolean sendHtmlMail(MailSenderInfo mailInfo) {

  Properties pro = mailInfo.getProperties();

  Session sendMailSession = Session.getInstance(pro);

  try {

  // 根据session创建一个邮件消息

  Message mailMessage = new MimeMessage(sendMailSession);

  // 创建邮件发送者地址

  Address from = new InternetAddress(mailInfo.getFromAddress());

  // 设置邮件消息的发送者

  mailMessage.setFrom(from);

  // 创建邮件的接收者地址,并设置到邮件消息中

  Address to = new InternetAddress(mailInfo.getToAddress());

  // Message.RecipientType.TO属性表示接收者的类型为TO

  mailMessage.setRecipient(Message.RecipientType.TO, to);

  // 设置邮件消息的主题

  mailMessage.setSubject(mailInfo.getSubject());

  // 设置邮件消息发送的时间

  mailMessage.setSentDate(new Date());

  // MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象

  Multipart mainPart = new MimeMultipart();

  // 创建一个包含HTML内容的MimeBodyPart

  BodyPart html = new MimeBodyPart();

  // 设置HTML内容

  html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");

  mainPart.addBodyPart(html);

  // 将MiniMultipart对象设置为邮件内容

  mailMessage.setContent(mainPart);

  // 发送邮件

Transport.send(mailMessage);

  return true;

  } catch (MessagingException ex) {

  if (ex.getCause() instanceof SendFailedException) {

  //发送失败, 更新发送状态为1.

  return false;

  }

  ex.printStackTrace();

  }

  return false;

  }

  /**

  * 以HTML格式发送邮件

  *

  * @param mailInfo

  *            待发送的邮件信息

  */

  public static boolean sendHtmlMail(MailSenderInfo mailInfo) {

  Properties pro = mailInfo.getProperties();

  Session sendMailSession = Session.getInstance(pro);

  try {

  // 根据session创建一个邮件消息

  Message mailMessage = new MimeMessage(sendMailSession);

  // 创建邮件发送者地址

  Address from = new InternetAddress(mailInfo.getFromAddress());

  // 设置邮件消息的发送者

  mailMessage.setFrom(from);

  // 创建邮件的接收者地址,并设置到邮件消息中

  Address to = new InternetAddress(mailInfo.getToAddress());

  // Message.RecipientType.TO属性表示接收者的类型为TO

  mailMessage.setRecipient(Message.RecipientType.TO, to);

  // 设置邮件消息的主题

  mailMessage.setSubject(mailInfo.getSubject());

  // 设置邮件消息发送的时间

  mailMessage.setSentDate(new Date());

  // MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象

  Multipart mainPart = new MimeMultipart();

  // 创建一个包含HTML内容的MimeBodyPart

  BodyPart html = new MimeBodyPart();

  // 设置HTML内容

  html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");

  mainPart.addBodyPart(html);

  // 将MiniMultipart对象设置为邮件内容

  mailMessage.setContent(mainPart);

  // 发送邮件

  Transport.send(mailMessage);

  return true;

  } catch (MessagingException ex) {

  if (ex.getCause() instanceof SendFailedException) {

  //发送失败, 更新发送状态为1.

  return false;

  }

  ex.printStackTrace();

  }

  return false;

  }


路过

鸡蛋

鲜花

握手

雷人

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 立即注册

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-28 23:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

返回顶部