发送邮件给多个收件人 [英] Sending mail to multiple recipient

查看:110
本文介绍了发送邮件给多个收件人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a simple interface that looks like below:

这很容易解释,我可以输入我的收件人电子邮件,它将值传递给函数.

It's pretty self-explanatory and I can enter my recipient email and it will pass the value to a function.

我的javascript函数(调用发送按钮时):

My javascript function(when send button invoked):

<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):

It passes to my 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());
        }    
    }

我上面的代码适用于单个收件人,现在我希望允许它像上面的屏幕快照所示的那样发送给多个收件人.出于某种原因,我不想创建抄送列.

My code above works for a single recipient and now I would like to allow it to send to multiple recipient like in the format of shown in the screenshot above. I do not want to create a CC column for reasons.

如何以所需的方式将多个电子邮件值传递给多个收件人?

How do I pass multiple values of emails to multiple recipients in the way I want?

推荐答案

您不需要使用"split".只需使用:

You don't need to use "split". Just use:

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

这篇关于发送邮件给多个收件人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆