2012-06-26 12 views
27

Sto provando a inviare posta ai miei amici tramite la mia applicazione Java Mail. Sono in grado di farlo con successo, tuttavia la colonna del destinatario nella casella di posta mostra l'indirizzo e-mail completo anziché il nome del mittente. Ho provato a modificare vari parametri, ma la casella di posta mostrava comunque l'indirizzo e-mail completo anziché il nome del mittente.L'indirizzo del mittente della posta Java visualizzato anziché il suo nome

utilizzando questo metodo per inviare il messaggio:

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

io chiamo questo metodo come:

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

risposta

69

È possibile impostare un nome nella InternetAddress utilizzando

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

la modalità di visualizzazione da campo è un dettaglio specifico client di attuazione.

Di solito se il mittente ha il formato "Sender Name" <[email protected]>, il client farà la cosa corretta in base alla configurazione.

Alcuni client deducono le informazioni sul nome dalle informazioni della loro rubrica se manca.

21

È necessario utilizzare two string constructor of InternetAddress per passare sia l'indirizzo di posta elettronica che il nome della persona. L'e-mail risultante conterrà una stringa come indicato da Jarrod.

InternetAddress fromAddress=new InternetAddress("[email protected]", "John Doe"); 
0

provare questo codice all'interno del metodo provare block.You possibile inizializzare il proprio nome all'interno del setFrom() di MIMEMessage.

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

cioè,

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

Anche se questo può teoricamente rispondere alla domanda, non è davvero una buona risposta, dal momento che non insegna l'OP. Invece dà una soluzione alternativa senza spiegazione. Questo di solito porta ad OP non a imparare e a tornare per fare una nuova domanda quando si verifica un problema simile. Ti dispiacerebbe aggiungere qualche spiegazione? – Vogel612

1

Le risposte di cui sopra sono corretti ma ho trovato avevo bisogno di mettere in un tentativo di cattura per farlo funzionare, ecco cosa ho trovato lavorato dall'applicazione sendemailwebapp demo.

Messaggio msg = new MimeMessage (sessione);

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

    } 
Problemi correlati