Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java mail sender's address displayed rather than his name

I am trying to send mail to my friends through my Java Mail application. I am able to do it successfully however the receiver's column in the mailbox shows the complete email address rather than the name of the sender. I tried changing various parameters but still the mailbox would show the full e-mail address rather than the name of the sender.

using this method to send the message:

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

I am calling this method as:

public static void main(String[] args) {     MailSender mailer=new MailSender();     mailer.send("[email protected]"); } 
like image 858
Nelo Angelo Avatar asked Jun 26 '12 16:06

Nelo Angelo


People also ask

What is InternetAddress in Java?

public InternetAddress(java.lang.String address, java.lang.String personal, java.lang.String charset) throws java.io.UnsupportedEncodingException. Construct an InternetAddress given the address and personal name. The address is assumed to be a syntactically valid RFC822 address.

What is Java mail name the protocols used in Java mail and explain?

The JavaMail is an API that is used to compose, write and read electronic messages (emails). The JavaMail API provides protocol-independent and plateform-independent framework for sending and receiving mails. The javax. mail and javax.

What is SMTP in Java?

SMTP is an acronym for Simple Mail Transfer Protocol. It is an Internet standard for electronic mail (e-mail) transmission across Internet Protocol (IP) networks. SMTP uses TCP port 25. SMTP connections secured by SSL are known by the shorthand SMTPS, though SMTPS is not a protocol in its own right.


2 Answers

You can set a name in the InternetAddress using

new InternetAddress("[email protected]", "Your Name"); 
like image 106
Raghav Avatar answered Sep 21 '22 17:09

Raghav


You should use the two string constructor of InternetAddress to pass in both the e-mail address and the person's name. The resulting e-mail will contain a string like Jarrod indicated.

InternetAddress fromAddress=new InternetAddress("[email protected]", "John Doe"); 
like image 27
Sarel Botha Avatar answered Sep 21 '22 17:09

Sarel Botha