Advertisement
Guest User

Untitled

a guest
Jan 12th, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. import java.util.Date;
  2. import java.util.Properties;
  3.  
  4. import javax.mail.Authenticator;
  5. import javax.mail.Message;
  6. import javax.mail.PasswordAuthentication;
  7. import javax.mail.Session;
  8. import javax.mail.Transport;
  9. import javax.mail.internet.InternetAddress;
  10. import javax.mail.internet.MimeMessage;
  11. /**
  12. * IPが海外だと送信できない
  13. * @author AYA
  14. *
  15. */
  16. public class SendMail {
  17. public static void main(String[] args) {
  18. try {
  19. // SMTPサーバー設定
  20. Properties props = System.getProperties();
  21. final String userName = "testzakki1206";
  22. final String password = "password";
  23. String host = "smtp.mail.yahoo.co.jp";
  24. String from = "testzakki1206@yahoo.co.jp";
  25. props.setProperty("mail.smtp.port", "587");
  26. props.setProperty("mail.smtp.auth", "true");
  27. props.put("mail.smtp.starttls.enable", "true");
  28. Session session = Session.getInstance(props, new Authenticator() {
  29. protected PasswordAuthentication getPasswordAuthentication() {
  30. return new PasswordAuthentication(userName, password);
  31. }
  32. });
  33. MimeMessage mimeMessage = new MimeMessage(session);
  34.  
  35. // 送信元メールアドレスと送信者名を指定
  36. mimeMessage.setFrom(new InternetAddress(userName + "@yahoo.co.jp",
  37. userName, "ISO-2022-JP"));
  38. // 送信先メールアドレスを指定
  39. mimeMessage.setRecipients(Message.RecipientType.TO,
  40. "zakki1206@yahoo.co.jp");
  41. // メールのタイトルを指定
  42. mimeMessage.setSubject("メールテスト", "ISO-2022-JP");
  43. // メールの内容を指定
  44. mimeMessage.setText("こんにちは\n", "ISO-2022-JP");
  45. // メールの形式を指定
  46. mimeMessage.setHeader("Content-Type", "text/html");
  47. // 送信日付を指定
  48. mimeMessage.setSentDate(new Date());
  49. // 送信します
  50. Transport transport = session.getTransport("smtp");
  51. transport.connect(host, from, password);
  52. transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
  53. transport.close();
  54. } catch (Exception e) {
  55. e.printStackTrace();
  56. }
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement