我正在尝试使用java邮件发送电子邮件通知程序。我能够向多个收件人发送邮件,但有多个主题。比如说。我正在向圣何塞用户发送未注册设备的数量,它应该将主题作为“未注册的圣何塞设备”,如果是Boulder用户则应该是'未注册的Boulder设备'。 这是主要方法。 公共类EmailNotifier实现Job {
private static Map<String,String>Site_names=new HashMap<String,String>();
private static Map<String,String> hostName_EmailAlias = new HashMap<String,String>();
private static Properties emailProperties;
private static Session mailSession;
private static MimeMessage emailMessage;
static {
hostName_EmailAlias.put("gigantic-6", "prvaidee@yahoo.com");
hostName_EmailAlias.put("ccm-sjcvtg-091", "vp@yahoo.com");
Site_names.put("gigantic-6", "UnRegistered Devices for San Jose");
Site_names.put("ccm-sjcvtg-091", "UnRegistered Devices for Pre-Alpha");
public void createSendEmailMessage(QueryDataObject dataObj) throws AddressException,
MessagingException, JobExecutionException {
String emailPort = "25";
String emailHost = "outbound.xxxxx.com";
emailProperties = System.getProperties();
emailProperties.put("mail.smtp.host", emailHost);
emailProperties.put("mail.smtp.port", emailPort);
emailProperties.put("mail.smtp.auth", "true");
emailProperties.put("mail.smtp.starttls.enable", "false");
String emailSubject = Site_names.get( "gigantic-6");
String emailSubject1=Site_names.get( "ccm-sjcvtg-091");
mailSession = Session.getDefaultInstance(emailProperties, null);
emailMessage = new MimeMessage(mailSession);
emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(dataObj.getEmailAlias()));
emailMessage.setSubject(emailSubject,emailSubject1);
emailMessage.setContent(dataObj.getEmailBody(), "text/html");// for a html email
Transport transport = mailSession.getTransport("smtp");
transport.connect(emailHost,emailSubject.emailSubject1);
//transport.connect(emailHost, fromUser, fromUserEmailPassword);
transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
transport.close();
System.out.println("Email sent successfully.");
}
我创建了2个地图,一个用于差异收件人的不同主机名,另一个用于差异主题(Sitenames)。我创建了2个字符串,其中包含emailSubject和emailSubject1.I我不知道如何将每个主机名和邮件与相应的主题相关联。 我不是要求完整的代码帮助。任何指针或想法都会有所帮助。
由于
答案 0 :(得分:1)
主题实际上是信息正文的一部分。您不能向多个收件人发送单个邮件,也不能更改每个收件人的内容。
对于每个不同的内容(包括主题),您必须创建单独的消息。如果该不同内容有多个收件人,则可以将该一封邮件发送给这些多个收件人。