2012-06-26 28 views
27

Tôi đang cố gắng gửi thư cho bạn bè thông qua ứng dụng Java Mail của mình. Tôi có thể thực hiện thành công tuy nhiên cột của người nhận trong hộp thư hiển thị địa chỉ email đầy đủ thay vì tên của người gửi. Tôi đã thử thay đổi các thông số khác nhau nhưng vẫn hộp thư sẽ hiển thị địa chỉ e-mail đầy đủ thay vì tên của người gửi.Địa chỉ của người gửi thư Java được hiển thị thay vì tên của anh ấy

sử dụng phương pháp này để gửi thông điệp:

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(); 
    } 
} 

Tôi gọi phương pháp này như:

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

Trả lời

69

Bạn có thể đặt một tên trong InternetAddress sử dụng

new InternetAddress("[email protected]", "Your Name"); 
3

như thế nào từ lĩnh vực được hiển thị là một khách hàng cụ thể chi tiết thực hiện.

Thông thường nếu người gửi ở dạng "Sender Name" <[email protected]>, khách hàng sẽ thực hiện điều đúng tùy thuộc vào cấu hình.

Một số khách hàng sẽ phỏng đoán thông tin tên từ thông tin sổ địa chỉ của họ nếu thiếu.

0

Hãy thử mã này trong thử block.You có thể khởi tạo tên của bạn trong setFrom() phương pháp của MimeMessage.

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

tức,

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

Trong khi điều này về mặt lý thuyết có thể trả lời câu hỏi, nó không thực sự là một câu trả lời tốt, vì nó không dạy OP. Thay vào đó nó đưa ra một giải pháp thay thế mà không cần giải thích. Điều này thường sẽ dẫn đến OP không học hỏi, và quay trở lại để hỏi một câu hỏi mới khi một vấn đề tương tự xảy ra. Bạn có nhớ thêm một số lời giải thích? – Vogel612

1

Những câu trả lời trên là đúng nhưng tôi thấy tôi cần phải đặt trong một catch cho nó hoạt động, đây là những gì tôi thấy làm việc từ ứng dụng sendemailwebapp demo.

Tin nhắn msg = new MimeMessage (phiên);

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)); 

    } 
Các vấn đề liên quan