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]"); }
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.
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.
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.
You can set a name in the InternetAddress
using
new InternetAddress("[email protected]", "Your Name");
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With