Send Email Out : EMail « Spring « Java






Send Email Out

       
File: context.xml



File: Main.java

import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;

public class Main {

  public static void main(String[] args) {
    String TO = "a@c.net";
    String JAVAMAIL_TEXT = "Hello World!.";

    SimpleMailSender sender1 = new JavaMailSimpleMailSender();

    sender1.sendMessage(TO, JAVAMAIL_TEXT);
  }
}

abstract class SimpleMailSender {
  protected abstract MailSender getMailSender();

  public void sendMessage(String to, String text) {
    SimpleMailMessage msg = new SimpleMailMessage();
    msg.setTo(to);
    msg.setSubject("Test Message");
    msg.setFrom("test@a.com");
    msg.setText(text);

    MailSender sender = getMailSender();
    try {
      sender.send(msg);
    } catch (MailException e) {
      e.printStackTrace();
    }
  }
}

class JavaMailSimpleMailSender extends SimpleMailSender {
  protected MailSender getMailSender() {
    JavaMailSenderImpl sender = new JavaMailSenderImpl();
    sender.setHost("post.yourserver.com");
    return sender;
  }
}




           
       








Spring-SendEmailOut.zip( 4,653 k)

Related examples in the same category