Hey,
ich möchte gerne, dass nicht nur die Email-Adresse als Absender dort steht, sondern der wirkliche Name (also "Max Mustermann" z.B.)
Wie realisier ich das?
ich möchte gerne, dass nicht nur die Email-Adresse als Absender dort steht, sondern der wirkliche Name (also "Max Mustermann" z.B.)
Wie realisier ich das?
Java:
package server;
import java.util.MissingResourceException;
import java.util.Properties;
import java.util.ResourceBundle;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendEmail {
private static String host;
private static String user;
private static String pass;
private static String port;
private static String from;
private static ResourceBundle mailSettings = ResourceBundle.getBundle("MailSettings");;
public static void init() {
host = getMailSettings("host");
user = getMailSettings("user");
pass = getMailSettings("pass");
port = getMailSettings("port");
from = getMailSettings("from");
System.out.print(from + " BLA");
}
/*
* returns a string from the MessageBundle-Properties-Files
*/
public static String getMailSettings(String key) {
try {
return mailSettings.getString(key);
} catch (MissingResourceException e) {
return '!' + key + '!';
}
}
public static void send(String to, String subject, String text){
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
props.put("mail.smtp.auth", "true");
Session mailSession = Session.getDefaultInstance(props);
Message simpleMessage = new MimeMessage(mailSession);
InternetAddress fromAddress = null;
InternetAddress toAddress = null;
try {
fromAddress = new InternetAddress(user);
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 transport = mailSession.getTransport("smtp");
transport.connect(host, user, pass);
simpleMessage.saveChanges();
transport.sendMessage(simpleMessage, simpleMessage.getAllRecipients());
transport.close();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}