Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Date;
- import java.util.Properties;
- import javax.mail.Authenticator;
- import javax.mail.Message;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- /**
- * IPが海外だと送信できない
- * @author AYA
- *
- */
- public class SendMail {
- public static void main(String[] args) {
- try {
- // SMTPサーバー設定
- Properties props = System.getProperties();
- final String userName = "testzakki1206";
- final String password = "password";
- String host = "smtp.mail.yahoo.co.jp";
- String from = "testzakki1206@yahoo.co.jp";
- props.setProperty("mail.smtp.port", "587");
- props.setProperty("mail.smtp.auth", "true");
- props.put("mail.smtp.starttls.enable", "true");
- Session session = Session.getInstance(props, new Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(userName, password);
- }
- });
- MimeMessage mimeMessage = new MimeMessage(session);
- // 送信元メールアドレスと送信者名を指定
- mimeMessage.setFrom(new InternetAddress(userName + "@yahoo.co.jp",
- userName, "ISO-2022-JP"));
- // 送信先メールアドレスを指定
- mimeMessage.setRecipients(Message.RecipientType.TO,
- "zakki1206@yahoo.co.jp");
- // メールのタイトルを指定
- mimeMessage.setSubject("メールテスト", "ISO-2022-JP");
- // メールの内容を指定
- mimeMessage.setText("こんにちは\n", "ISO-2022-JP");
- // メールの形式を指定
- mimeMessage.setHeader("Content-Type", "text/html");
- // 送信日付を指定
- mimeMessage.setSentDate(new Date());
- // 送信します
- Transport transport = session.getTransport("smtp");
- transport.connect(host, from, password);
- transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
- transport.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement