2017-09-02 22 views
1

メールを送信するためにJavaメールAPIを使用しています。私は私の例ではいずれかが私を助けることができる、送信者のメールアドレスが[email protected]ですが、私はそれが受信トレイ に他の名前になりたいここ送信者名の代わりに送信者名を受信者名に設定する方法

import java.util.Properties; 
import javax.mail.*; 
import javax.mail.internet.*; 

public class send{ 
public static void main(String[] args) { 

    String host="smtp.gmail.com"; 
    final String from="[email protected]";//change accordingly 
    final String password="12345";//change accordingly 

    String to="[email protected]";//change accordingly 

    //Get the session object 
    Properties props = new Properties();  
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.smtp.starttls.enable", "true"); 
    props.put("mail.smtp.host", host); 
    props.put("mail.smtp.port", "587"); 

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

    //Compose the message 
    try { 
    MimeMessage message = new MimeMessage(session); 
    message.setFrom(new InternetAddress(from)); 
    message.addRecipient(Message.RecipientType.TO,new InternetAddress(to)); 
    message.setSubject("Test"); 
    message.setText("hello"); 
    //send the message 
    Transport.send(message); 

    System.out.println("message sent successfully..."); 

    } catch (MessagingException e) {e.printStackTrace();} 
} 
} 

私の例である他の名前であることを、送信者名、送信者名をしたいですか?

答えて

0

InternetAddressは次のように使用することができます。今の代わり[email protected]InternetAddress(mail, alias)、あなたが

message.setFrom(new InternetAddress(from, "Your Name")); 

message.setFrom(new InternetAddress(from)); 

を変更したいという意味受信者は送信者名としてYour Name表示されます。

+0

私はどこからprogarammのアドレスを渡すべきですか?既にアドレスから私のプログラムの中にあります – bharath

+0

答えを確認し、より明確にしました。 – campovski

+0

ええ、John Doe <[email protected]>のように登場します。私はこれをJohn Doe にしたいと思います。また、inboxでもsendmailの名前は[email protected]のようになりますが、John [email protected]のようにはなりません。 – bharath

関連する問題