向多个收件人发送邮件
Sending mail to multiple recipient
我有一个简单的界面,如下所示:
它非常不言自明,我可以输入我的收件人电子邮件,它会将值传递给一个函数。
我的 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());
}
}
我上面的代码适用于单个收件人,现在我想允许它发送给多个收件人,如上面屏幕截图所示的格式。由于某些原因,我不想创建抄送专栏。
如何以我想要的方式将电子邮件的多个值传递给多个收件人?
您可以根据 ,
拆分您的收件人电子邮件列表,然后遍历每封电子邮件并发送给每一封。如:
UPDATE:您不需要在循环中调用 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());
}
}
您不需要使用 "split"。只需使用:
message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));
我有一个简单的界面,如下所示:
它非常不言自明,我可以输入我的收件人电子邮件,它会将值传递给一个函数。
我的 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());
}
}
我上面的代码适用于单个收件人,现在我想允许它发送给多个收件人,如上面屏幕截图所示的格式。由于某些原因,我不想创建抄送专栏。
如何以我想要的方式将电子邮件的多个值传递给多个收件人?
您可以根据 ,
拆分您的收件人电子邮件列表,然后遍历每封电子邮件并发送给每一封。如:
UPDATE:您不需要在循环中调用 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());
}
}
您不需要使用 "split"。只需使用:
message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));