1. <tt id="5hhch"><source id="5hhch"></source></tt>
    1. <xmp id="5hhch"></xmp>

  2. <xmp id="5hhch"><rt id="5hhch"></rt></xmp>

    <rp id="5hhch"></rp>
        <dfn id="5hhch"></dfn>

      1. Java認證考試:Spring實現郵件發送

        時間:2024-07-29 10:23:33 JAVA認證 我要投稿
        • 相關推薦

        Java認證考試:Spring實現郵件發送

          Spring提供了一個發送郵件的抽象層,使發送郵件實現非常簡單。下面代碼需要mail.jar包,如果服務器需要認證,必須加入如下加粗代碼:

        Java認證考試:Spring實現郵件發送

          源文件: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 {

          //郵件發送器

          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;

          //需要認證

          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);

          //創建發送器

          JavaMailSenderImpl sender = new JavaMailSenderImpl();

          sender.setHost(host);

          sender.setUsername(username);

          sender.setPassword(pass);

          //創建消息

          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());

          //開始發送

          sender.setJavaMailProperties(props);

          sender.send(message);

          }catch(Exception e){

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

          return “Failure”;

          }

          return “Success”;

          }

          //測試

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

          {

          String subject = “測試郵件”;//標題

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

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

          String user = “send@my.com”;//發送者用戶

          String pw = “password”;//發送者郵件密碼

          String fromName = “Chen”;//發送者名字

          String protocol = “smtp”;//協議

          String host = “smtp.my.com”;//發送主機

          String port = “25”;//端口

          String msg = “簡單郵件發送。”;//發送內容

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

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

          }

          }

          使用MimeMessageHelper,可以實現Multipart email,方便添加附件和內嵌資源等。

        【Java認證考試:Spring實現郵件發送】相關文章:

        Spring如何實現郵件發送07-26

        JAVA認證考試細則06-10

        java認證考試介紹07-31

        sun java認證考試介紹10-23

        JAVA認證考試費用06-25

        java認證考試細則介紹08-19

        Sun java認證考試答案11-06

        JAVA考試認證經驗分享08-22

        Linux認證系統管理:linuxmail命令發送郵件失敗09-21

        Sun Java認證考試科目08-30

        国产高潮无套免费视频_久久九九兔免费精品6_99精品热6080YY久久_国产91久久久久久无码

        1. <tt id="5hhch"><source id="5hhch"></source></tt>
          1. <xmp id="5hhch"></xmp>

        2. <xmp id="5hhch"><rt id="5hhch"></rt></xmp>

          <rp id="5hhch"></rp>
              <dfn id="5hhch"></dfn>