I am sending mail with Java mail and an SMTP server. I want to be able to change the "name" that the recipient sees when they get an email message - not simply the prefix of the email address (the bit before @).
I suspect I need to change or add one of the 'props.put();' settings but I can't work out which one.
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]","me@myservercom"};
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();
}
}
}
The name attribute specifies a name for an HTML element. This name attribute can be used to reference the element in a JavaScript. For a <form> element, the name attribute is used as a reference when the data is submitted. For an <iframe> element, the name attribute can be used to target a form submission.
The name attribute specifies the name of an <input> element. The name attribute is used to reference elements in a JavaScript, or to reference form data after a form is submitted. Note: Only form elements with a name attribute will have their values passed when submitting a form.
The <address> tag defines the contact information for the author/owner of a document or an article. The contact information can be an email address, URL, physical address, phone number, social media handle, etc.
You need to change:
message.setFrom(new InternetAddress(FROM));
to
message.setFrom(new InternetAddress(FROM, "Company XYZ"));
Documentation: Class InternetAddress
InternetAddress
public InternetAddress(String address, String personal) throws UnsupportedEncodingException
Construct an InternetAddress given the address and personal name. The address is assumed to be a syntactically valid RFC822 address.
Parameters:
address - the address in RFC822 format
personal - the personal name
Throws: UnsupportedEncodingException
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