Vert.x 傳送郵件

sweetmain發表於2017-12-13

程式碼不多直接上

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.ext.mail.MailClient;
import io.vertx.ext.mail.MailConfig;
import io.vertx.ext.mail.MailMessage;
import io.vertx.ext.mail.StartTLSOptions;

/**
 *
 * Created by sweet on 2017/9/15.
 */
public class MailVerticle extends AbstractVerticle {

  public static void main(String[] args) {
    Runner.runExample(MailVerticle.class);
  }

  @Override
  public void start(Future<Void> startFuture) throws Exception {

    MailConfig config = new MailConfig();
    config.setHostname("smtp.163.com");
    config.setSsl(true);
    config.setPort(465);
    config.setStarttls(StartTLSOptions.REQUIRED);
    config.setUsername("xxxxxx@163.com");
    config.setPassword("123456");
    MailClient mailClient = MailClient.createShared(vertx, config, "xxxx");

    MailMessage message = new MailMessage();
    message.setFrom("xxxxxx@163.com");
    message.setTo("xxxxxx@163.com");
    message.setCc("jieshouzhe@qq.com"); // 接受者

    message.setSubject("Vertx");
    message.setText("Hello World From Sweet");
    mailClient.sendMail(message, result -> {
      if (result.failed()){
        result.cause().printStackTrace();
        System.out.println(result.cause().getMessage());
        return;
      }
      String messageID = result.result().getMessageID();
      System.out.println("id: " + messageID);
      System.out.println(result.result().toString());
      System.out.println("-----------------------");
      result.result().getRecipients().forEach(System.out::println);
    });
    
    startFuture.complete();
  }
}
複製程式碼

Runner的程式碼在我之前的文章裡有地址。

注意: Subject 不要有特殊字元。

相關文章