国产激情久久久久影院小草_国产91高跟丝袜_99精品视频99_三级真人片在线观看

Java認(rèn)證考試:Spring實(shí)現(xiàn)郵件發(fā)送

時(shí)間:2024-07-29 10:23:33 JAVA認(rèn)證 我要投稿
  • 相關(guān)推薦

Java認(rèn)證考試:Spring實(shí)現(xiàn)郵件發(fā)送

  Spring提供了一個(gè)發(fā)送郵件的抽象層,使發(fā)送郵件實(shí)現(xiàn)非常簡(jiǎn)單。下面代碼需要mail.jar包,如果服務(wù)器需要認(rèn)證,必須加入如下加粗代碼:

Java認(rèn)證考試:Spring實(shí)現(xiàn)郵件發(fā)送

  源文件:SendMail.java:

  package mail;

  import org.springframework.mail.javamail.JavaMailSenderImpl;

  import org.springframework.mail.javamail.MimeMessageHelper;

  import javax.mail.internet.MimeMessage;

  import java.util.Properties;

  import java.util.Date;

  /**

  * @author chrischen

  */

  public class SendMail {

  //郵件發(fā)送器

  public static String Sender(String subject, String msg, String sendTo, String fromMail, String user, String pw, String fromName, String protocol, String host, String port){

  try{

  final String username = user;

  final String pass = pw;

  //需要認(rèn)證

  Properties props = new Properties();

  props.put(“mail.smtp.host”, host);

  props.put(“mail.smtp.auth”, “true”);

  props.put(“mail.transport.protocol”, protocol);

  props.put(“mail.from”, fromMail);

  //創(chuàng)建發(fā)送器

  JavaMailSenderImpl sender = new JavaMailSenderImpl();

  sender.setHost(host);

  sender.setUsername(username);

  sender.setPassword(pass);

  //創(chuàng)建消息

  MimeMessage message = sender.createMimeMessage();

  message.addHeader(“X-Mailer”, “Java Mailer”);

  MimeMessageHelper helper = new MimeMessageHelper(message);

  helper.setTo(sendTo);

  helper.setFrom(fromMail, fromName);

  helper.setSubject(subject);

  helper.setText(msg);

  helper.setSentDate(new Date());

  //開(kāi)始發(fā)送

  sender.setJavaMailProperties(props);

  sender.send(message);

  }catch(Exception e){

  System.out.println(“Error:” + e);

  return “Failure”;

  }

  return “Success”;

  }

  //測(cè)試

  public static void main(String args[])throws Exception

  {

  String subject = “測(cè)試郵件”;//標(biāo)題

  String sendTo = “test@my.com”;//接收者郵件

  String fromMail = “send@my.com”;//發(fā)送者郵件

  String user = “send@my.com”;//發(fā)送者用戶(hù)

  String pw = “password”;//發(fā)送者郵件密碼

  String fromName = “Chen”;//發(fā)送者名字

  String protocol = “smtp”;//協(xié)議

  String host = “smtp.my.com”;//發(fā)送主機(jī)

  String port = “25”;//端口

  String msg = “簡(jiǎn)單郵件發(fā)送。”;//發(fā)送內(nèi)容

  String ret = Sender(subject, msg, sendTo, fromMail, user, pw, fromName, protocol, host, port);

  System.out.println(“郵件發(fā)送結(jié)果:” + ret);

  }

  }

  使用MimeMessageHelper,可以實(shí)現(xiàn)Multipart email,方便添加附件和內(nèi)嵌資源等。

【Java認(rèn)證考試:Spring實(shí)現(xiàn)郵件發(fā)送】相關(guān)文章:

Spring如何實(shí)現(xiàn)郵件發(fā)送07-26

JAVA認(rèn)證考試細(xì)則06-10

java認(rèn)證考試介紹07-31

sun java認(rèn)證考試介紹10-23

JAVA認(rèn)證考試費(fèi)用06-25

java認(rèn)證考試細(xì)則介紹08-19

Sun java認(rèn)證考試答案11-06

JAVA考試認(rèn)證經(jīng)驗(yàn)分享08-22

Linux認(rèn)證系統(tǒng)管理:linuxmail命令發(fā)送郵件失敗09-21

Sun Java認(rèn)證考試科目08-30