2012-04-16 8 views
7

Wysyłam pocztę z pocztą Java i serwerem SMTP. Chcę móc zmienić "nazwę", którą widzi odbiorca po otrzymaniu wiadomości e-mail - nie tylko prefiks adresu e-mail (bit przed @).Jak ustawić atrybut "nazwa" w wiadomości e-mail?

Podejrzewam, że muszę zmienić lub dodać jedną z opcji "props.put();" ustawienia, ale nie mogę ustalić, który z nich.

public class Email { 

    private final String HOST = "mail.myserverr.com"; 
    private final String USER = "me+myserver.com"; 
    private final String FROM = "[email protected]"; 
    private final String PASS = "mypass"; 
    private final String PORT = "25"; 
    private final String AUTH = "true"; 

    @Test 
    public void sendMail(){ 
     String[] to = {"[email protected]","[email protected]"}; 
     sendMessage(to,"Let's go","What's up"); 

    } 

    public void sendMessage(String[] to, String subject, String msg) { 

     Properties props = System.getProperties(); 
      props.put("mail.smtp.starttls.enable", "true"); // added this line 
      props.put("mail.smtp.host", HOST); 
      props.put("mail.smtp.user", USER); 
      props.put("mail.smtp.password", PASS); 
      props.put("mail.smtp.port", PORT); 
      props.put("mail.smtp.auth", AUTH); 
      props.put("mail.smtp.socketFactory.port", PORT); 
      props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
      props.put("mail.smtp.socketFactory.fallback", "false"); 


     Session session = Session.getDefaultInstance(props, null); 
     MimeMessage message = new MimeMessage(session); 
     try { 
      message.setFrom(new InternetAddress(FROM)); 

     InternetAddress[] toAddress = new InternetAddress[to.length]; 

     // To get the array of addresses 
     for(int i=0; i < to.length; i++) { // changed from a while loop 
      toAddress[i] = new InternetAddress(to[i]); 
     } 

     for(int i=0; i < toAddress.length; i++) { // changed from a while loop 
      message.addRecipient(Message.RecipientType.TO, toAddress[i]); 
     } 

     message.setSubject(subject); 
     message.setText(msg); 

     Transport transport = session.getTransport("smtps"); 
     transport.connect(HOST, USER, PASS); 
      transport.sendMessage(message, message.getAllRecipients()); 
      transport.close(); 

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

Odpowiedz

7

trzeba zmienić:

message.setFrom(new InternetAddress(FROM)); 

do

message.setFrom(new InternetAddress(FROM, "Company XYZ")); 

Dokumentacja: Class InternetAddress

InternetAddress

public InternetAddress(String address, 
        String personal) 
        throws UnsupportedEncodingException 

Skonstruuj InternetAddress podany adres i nazwa osobisty. Adres jest założony jako poprawny składnie adres RFC822.

Parametry:

Address - Adres w formacie RFC822

osobista - osobista nazwa

Zgłasza: UnsupportedEncodingException

+0

Dzięki bardzo. – Ankur

23

Typowa składnia adres ma postać "[email protected]" lub "Personal Name <[email protected]>".
Możesz użyć tej samej składni dla adresów pól FROM i TO.

Przykład:
Zmień następujące oświadczenie:
String[] to = {"[email protected]","[email protected]"};
do
String[] to = {"Recipient1 Name <[email protected]>","My Name <[email protected]>"};

Można także zbudować obiekty InternetAddress przechodzących nazwami e-mailID i osobiste jako argumenty.
Przykład:

String FROM = "[email protected]"; 
InternetAddress from = new InternetAddress(FROM, "Ravinder"); 

odbiorca zobaczy nazwę nadawcy na wyświetlaczu "Ravinder" zamiast "[email protected]"

referencyjny:javax.mail.internet.InternetAddress

+0

Dzięki za dodatkowe informacje o zmianie nazwy odbiorcy. – Ankur

+0

@Ankur: Mam nadzieję, że uzyskałeś właściwą odpowiedź. [* Zaakceptowanie odpowiedzi: Jak to działa? *] (Http: //meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235 # 5235) –

+0

Czy można całkowicie ukryć identyfikator e-mail? nawet po najechaniu kursorem – Sadanand

Powiązane problemy