2012-03-19 17 views
1

私はメールアプリケーションの問題に悩まされました。私は学問目的のウェブサイトを開発しています。 Javaを使って開発しています。どのようにパスワード認証を使わずにGmailサービス/その他を使ってメールを送ることができますか?gmail smtpサービスを使用してメールを送信するには?

おかげに関して 教祖Bhatさん

+0

あなたはもう少し説明する必要があります。ユーザーの代わりに電子メールを送信しますか? Gmailアカウントを持っているあなたのサービスからのメール?または... – Ali

答えて

2

は、プロジェクト内のmail.jarを追加して、以下の設定を行います。
送信メール(SMTP)サーバー
は、TLSまたはSSLが必要です:smtp.gmail.com(使用認証)
利用認証:TLS/STARTTLSのためにはい
ポート:587
ポートSSL用:465

import java.util.Properties; 

import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 

public class SendMailTLS { 

public static void main(String[] args) { 

    final String username = "[email protected]"; 
    final String password = "password"; 

    Properties props = new Properties(); 
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.smtp.starttls.enable", "true"); 
    props.put("mail.smtp.host", "smtp.gmail.com"); 
    props.put("mail.smtp.port", "587"); 

    Session session = Session.getInstance(props, 
     new javax.mail.Authenticator() { 
     protected PasswordAuthentication getPasswordAuthentication() { 
      return new PasswordAuthentication(username, password); 
     } 
     }); 

    try { 

     Message message = new MimeMessage(session); 
     message.setFrom(new InternetAddress("[email protected]")); 
     message.setRecipients(Message.RecipientType.TO, 
      InternetAddress.parse("[email protected]")); 
     message.setSubject("Testing Subject"); 
     message.setText("Dear Mail Crawler," 
      + "\n\n No spam to my email, please!"); 

     Transport.send(message); 

     System.out.println("Done"); 

    } catch (MessagingException e) { 
     throw new RuntimeException(e); 
    } 
} 
    } 

問題が続く場合はthis link

関連する問題