2012-12-01 4 views
6

setFrom()メソッドをどのように変更したいのですか? Gmailの添付ファイルを使って電子メールを送信し、setFromテキストを変更することができますが、メールにはusernameが表示されます。私もyahooアカウントを使ってみましたが、認証エラーが出ます。Javamail API - setFromをどのように変更しますか?

送信元アドレスを変更したいと思います。コードは以下の通りである:

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); 
     } 
    } 
} 
+0

Googleのリレーを使用しないでください。 – Perception

+2

私の友人、あなたのメールを送信するためにGoogleリレーを使用しています( 'props.put(" mail.smtp.host "、" smtp.gmail.com ");')。あなたのユーザ名が発信元アドレスとして表示されることをあまり厳しくしない場合は、別のリレーを使用してください。 – Perception

+0

あなたがやろうとしていることは、一般的に「スパム」と呼ばれています。 :-)だからこそGmailはあなたにそれをさせません。正当な理由がある場合は、次のGmailのヘルプページをご覧ください:[別のアドレスからメールを送信](https://support.google.com/mail/bin/answer.py?hl=ja&answer=22370) –

答えて

0

多くの評判のSMTPリレーが偽のあなたの身元(それはそれがさらに簡単にスパマーがサービスを悪用するために作るでしょう)にあなたを許しません。あなたの目的があなたのインボックスのメールを避けることだとすれば、Google allows you to modify your email addressはあなたのメールへの返信をフィルタできるようになりました。

-2

ここに完全なコード 次のコードを使用できます。 これは私

"smtp.gmail.com" を使用しながら、同じ問題に直面した
class SendMail { 
public static void main(String[] args) { 
    System.out.println("In side main()---------------------"); 

    Properties props = new Properties(); 


    props.put("mail.smtp.host", "hostname"); 
    props.put("mail.smtp.port", "port");// 

    props.put("mail.smtp.socketFactory.port", "port"); 
    props.put("mail.smtp.socketFactory.class", 
      "javax.net.ssl.SSLSocketFactory"); 



    props.put("mail.smtp.auth", "true"); 

    Session session = Session.getDefaultInstance(props, 
      new javax.mail.Authenticator() { 

       protected PasswordAuthentication getPasswordAuthentication() { 
        System.out 
          .println("In side getPasswordAuthentication------------------And Before returning PasswordAuthentication"); 
        return new PasswordAuthentication("[email protected]", 
          "password"); 

       } 

      }); 
    System.out 
      .println("mail and password has been sent********************"); 
    try { 
     System.out 
       .println("we are in try{} block................................."); 
     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 User," + "\n\n This is testing only!"); 

     Transport.send(message); 

     System.out 
       .println("Mail has been sent successfully.........................."); 

    } catch (MessagingException e) { 
     System.out.println("we are in catch block..................."); 
     e.printStackTrace(); 

    } 
} 

}

+0

http://www.oracle.com/technetwork/java/faq-135477.html#commonmistakes – BalusC

1

の罰金を働いています。 マンドリルを使用すると動作します。 マンドリルアカウントを設定したら、ポート587で "smtp.mandrillapp.com"を使用します。 認証の場合、アカウントで生成されたusername = mandrillのユーザー名とパスワード= APIキーを設定します。

0

(あなたの目標を理解しており、迷惑メールを送信しないと仮定します)私たちが使用するApache Commonsライブラリは、InternetAddresssetPersonalを呼び出して行います。 http://docs.oracle.com/javaee/7/api/javax/mail/internet/InternetAddress.html#setPersonal-java.lang.String-java.lang.String-

 Message message = new MimeMessage(session); 
     InternetAddress me = new InternetAddress("[email protected]"); 
     me.setPersonal("My name"); 
     message.setFrom(me);