gpt4 book ai didi

java - 向多个收件人发送邮件

转载 作者:行者123 更新时间:2023-12-02 01:34:19 27 4
gpt4 key购买 nike

我有一个简单的界面,如下所示:

enter image description here

这是非常不言自明的,我可以输入我的收件人电子邮件,它会将值传递给函数。

我的 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(片段):

<%   
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());
}
}

上面的代码适用于单个收件人,现在我想允许它以上面屏幕截图所示的格式发送给多个收件人。由于某些原因,我不想创建 CC 专栏。

如何按照我想要的方式将电子邮件的多个值传递给多个收件人?

最佳答案

您不需要使用“split”。只需使用:

message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));

关于java - 向多个收件人发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55467579/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com