我有一个简单的界面,如下所示:
这很不言自明,我可以输入我的收件人电子邮件,它将值传递给函数。
我的javascript函数(调用发送按钮时):
<script>
//function to insert content of email to table
function sendmessage(){
var recipient = document.getElementById("recipient").value;
var subject = document.getElementById("subject").value;
var content=document.getElementById("content").value;
$.ajax({
url: 'sendemail.jsp',
type: 'POST',
data: {
recipient:recipient,
subject:subject,
content:content
},
success: function (data) {
alert("Successfully send email");
},
error: function (request, error) {
alert("Request: " + JSON.stringify(error));
}
});
}
</script>
<body>
To:<input type="text" style="font-size: 10pt;" size="30" id="recipient" ><br><br>
Subject:<input type="text" style="font-size: 10pt" size="70" id="subject" ><br><br>
Content:<br><textarea cols="100" rows="10" id="content" style="font-size: 13pt;">
<%=files%>: <%=url%>
</textarea><br>
<div class="Send">
<button type="button" onclick="sendmessage()"> Send </button>
</div>
它传递到我的sendemail.jsp(snippet):
<%
String recipient=request.getParameter("recipient");
String subject=request.getParameter("subject");
String content=request.getParameter("content");
fileFacade.sendEmail(recipient,subject,content);
%>
我的电子邮件功能:
public void sendEmail(String recipient,String subject,String content) {
try {
final String fromEmail = "xxxxx@gmail.com"; //requires valid gmail id
final String password = "xxxx"; // correct password for gmail id
System.out.println("Please Wait, sending email...");
/*Setup mail server */
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com"); //SMTP Host
props.put("mail.smtp.port", "25"); //TLS Port
props.put("mail.smtp.auth", "true"); //enable authentication
props.put("mail.smtp.starttls.enable", "true"); //enable STARTTLS
//create Authenticator object to pass in Session.getInstance argument
Authenticator auth = new Authenticator() {
//override the getPasswordAuthentication method
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(fromEmail, password);
}
};
Session session = Session.getInstance(props, auth);
session.setDebug(true);
// Define message
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(fromEmail));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
// Set Subject: header field
message.setSubject(subject);
// Now set the actual message
message.setText(content);
try {
Transport.send(message);
} catch (AddressException addressException) {
addressException.printStackTrace();
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
我上面的代码适用于单个收件人,现在我想允许它以上面的屏幕快照所示的格式发送给多个收件人。出于某种原因,我不想创建抄送列。
如何以所需的方式将多个电子邮件值传递给多个收件人?
答案 0 :(得分:1)
您可以根据,
拆分收件人电子邮件列表,然后遍历每封电子邮件并发送给每封电子邮件。为:
更新:您无需循环调用send
方法。只需多次添加recipeints即可一次将电子邮件发送给所有人。
String[] receipentList = recipient.split(",");
for (String to : receipentList) {
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
}
try {
Transport.send(message);
} catch (AddressException addressException) {
addressException.printStackTrace();
}
完整代码:
public void sendEmail(String recipient, String subject, String content) {
try {
final String fromEmail = "xxxxx@gmail.com"; //requires valid gmail id
final String password = "xxxx"; // correct password for gmail id
System.out.println("Please Wait, sending email...");
/*Setup mail server */
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com"); //SMTP Host
props.put("mail.smtp.port", "25"); //TLS Port
props.put("mail.smtp.auth", "true"); //enable authentication
props.put("mail.smtp.starttls.enable", "true"); //enable STARTTLS
//create Authenticator object to pass in Session.getInstance argument
Authenticator auth = new Authenticator() {
//override the getPasswordAuthentication method
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(fromEmail, password);
}
};
Session session = Session.getInstance(props, auth);
session.setDebug(true);
// Define message
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(fromEmail));
// Set Subject: header field
message.setSubject(subject);
// Now set the actual message
message.setText(content);
String[] receipentList = recipient.split(",");
for (String to : receipentList) {
message.addRecipient(Message.RecipientType.TO, new
InternetAddress(to));
}
try {
Transport.send(message);
} catch (AddressException addressException) {
addressException.printStackTrace();
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
答案 1 :(得分:1)
您不需要使用“拆分”。只需使用:
message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));