2012-06-26 35 views
27

我試圖通過我的Java郵件應用程序向我的朋友發送郵件。我可以成功完成,但郵箱中的收件人列顯示的是完整的電子郵件地址,而不是發件人的姓名。我嘗試更改各種參數,但郵箱仍會顯示完整的電子郵件地址,而不是發件人的名稱。顯示的是Java郵件發件人的地址而不是他的名字

使用此方法發送的消息:

public void send(String key){ 
    String to=key; 
    String from="mygmailid"; 
    String subject="wassp"; 
    String text="Hello"; 
    Properties props=new Properties(); 

    props.put("mail.smtp.host", "smtp.gmail.com"); 
    props.put("mail.smtp.user", "myname"); 
    props.put("mail.smtp.socketFactory.port", "465"); 
    props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory"); 
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.smtp.port", "465"); 
    Session mailSession=Session.getDefaultInstance(props); 
    Message simpleMessage=new MimeMessage(mailSession); 

    InternetAddress fromAddress=null; 
    InternetAddress toAddress=null; 

    try{ 
     fromAddress=new InternetAddress(from); 
     toAddress=new InternetAddress(to); 
    } 
    catch(AddressException e){ 
     e.printStackTrace(); 
    } 

    try{ 
     simpleMessage.setFrom(fromAddress); 
     simpleMessage.setRecipient(RecipientType.TO,toAddress); 
     simpleMessage.setSubject(subject); 
     simpleMessage.setText(text); 

     transport.connect("smtp.gmail.com",465, "[email protected]", "mygmailpassword"); 
     transport.sendMessage(simpleMessage, simpleMessage.getAllRecipients()); 
     transport.close(); 

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

我調用此方法爲:

public static void main(String[] args) { 
    MailSender mailer=new MailSender(); 
    mailer.send("[email protected]"); 
} 

回答

3

如何從字段顯示是一個客戶端具體的實施細節。

通常,如果發件人的形式爲"Sender Name" <[email protected]>,則客戶端將根據配置執行正確的操作。

如果某些客戶端丟失了某些客戶端的名稱信息,他們將從其地址簿信息中推斷出該名稱信息。

0

嘗試的MimeMessage的嘗試block.You可以setFrom內初始化您的姓名()方法中的代碼設置在InternetAddress的名稱。上述

simpleMessage.setFrom(new InternetAddress("Your mail id", "Your name")); 

try{ 
    simpleMessage.setFrom(new InternetAddress("Your mail id", "Your name")); 
    simpleMessage.setRecipient(RecipientType.TO,toAddress); 
    simpleMessage.setSubject(subject); 
    simpleMessage.setText(text); 

    transport.connect("smtp.gmail.com",465, "[email protected]", "mygmailpassword"); 
    transport.sendMessage(simpleMessage, simpleMessage.getAllRecipients()); 
    transport.close(); 

} 
+0

雖然這在理論上回答這個問題,這是不是真的一個很好的答案,因爲它不教OP。相反,它給出了一個沒有解釋的替代方案這通常會導致OP沒有學習,並且在發生類似問題時回到問一個新問題。你介意添加一些解釋嗎? – Vogel612

1

的答案是正確的,但我發現我需要在一個嘗試捕捉到的地方它的工作,這裏是我的發現從sendemailwebapp演示應用程序的工作。

消息msg = new MimeMessage(session);

try { 
     msg.setFrom(new InternetAddress(userName, "YourName")); 
    } catch (UnsupportedEncodingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    InternetAddress[] toAddresses = { new InternetAddress(toAddress) }; 
    msg.setRecipients(Message.RecipientType.TO, toAddresses); 
    msg.setSubject(subject); 
    msg.setSentDate(new Date()); 
    msg.setText(message); 
3
try { 

     String from = " EMAIL ID"; 
     String SMTP_AUTH_PWD = " PASSWORD "; 
     Properties props = new Properties(); 
     props.put("mail.smtp.host", "smtp.gmail.com"); 
     props.put("mail.smtp.starttls.enable", "true"); 
     props.put("mail.transport.protocol", "smtps"); 
     props.put("mail.smtps.auth", "true"); 
     String SMTP_HOST_NAME = "smtp.gmail.com"; 
     int SMTP_HOST_PORT = 465; 
     javax.mail.Session mailSession = Session.getDefaultInstance(props); 

     mailSession.setDebug(true); 
     Transport transport = ((javax.mail.Session) mailSession) 
       .getTransport(); 

     javax.mail.Message message = new MimeMessage(mailSession); 
     message.setSubject("Testing SMTP-SSL"); 
     message.setContent("", "text/plain"); 
     message.addRecipient(javax.mail.Message.RecipientType.TO, 
       new InternetAddress(receiver)); 
     transport.connect(SMTP_HOST_NAME, SMTP_HOST_PORT, from, 
       SMTP_AUTH_PWD); 
     message.setFrom(new InternetAddress(from," YOUR PREFERED NAME ")); 
     message.setSubject(subject); 
     BodyPart messageBodyPart = new MimeBodyPart(); 
     messageBodyPart.setText(body); 
     Multipart multipart = new MimeMultipart(); 
     multipart.addBodyPart(messageBodyPart); 
     messageBodyPart = new MimeBodyPart(); 
     message.setContent(multipart); 

     transport.sendMessage(message, 
       message.getRecipients(javax.mail.Message.RecipientType.TO)); 

    } 
相關問題