【问题标题】:Java adding CC and BCC to send email programJava添加CC和BCC发送邮件程序
【发布时间】:2014-01-17 03:43:10
【问题描述】:

我有以下代码,可以从 gmail 发送邮件。问题是我应该在哪个部分添加抄送和密送收件人,以便他们也可以接收邮件。

我进行了搜索并在这里找到了一些帮助,但它在我的程序上不起作用,我在密件抄送和抄送列表上收到错误。

    InternetAddress[] myToList = InternetAddress.parse("gopi.mani@xyz.com,Maimsa.SF@xyz.com"); 
    InternetAddress[] myBccList = InternetAddress.parse("Usha.B@xyz.com"); 
    InternetAddress[] myCcList = InternetAddress.parse("NEHA.SIVA@xyz.com"); 


    message.setRecipients(Message.RecipientType.TO,myToList); 
    message.addRecipient(Message.RecipientType.BCC,myBccList); 
    message.addRecipient(Message.RecipientType.CC,myCcList);

代码

 import javax.mail.*;
    import javax.mail.internet.*;
    import java.util.*;
    
    public class SendEmail {
    
        private static final String SMTP_HOST_NAME = "smtp.gmail.com";
        private static final int SMTP_HOST_PORT = 587;
        private static final String SMTP_AUTH_USER = "sender@gmail.com";
        private static final String SMTP_AUTH_PWD = "";
    
        public static void main(String[] args) throws Exception {
            SendEmail se = new SendEmail();
            se.mail();
        }
    
        public void mail() throws Exception {
            Properties props = new Properties();
            props.put("mail.smtp.host", "smtp.gmail.com");
            props.put("mail.smtp.socketFactory.port", "465");
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.port", "465");
    
            Session mailSession = Session.getDefaultInstance(props);
            mailSession.setDebug(true);
            Transport transport = mailSession.getTransport();
            MimeMessage message = new MimeMessage(mailSession);
            message.setSubject("Testing SMTP From Java");
    
            message.setContent("Hello world", "text/plain");
    
            message.addRecipient(Message.RecipientType.TO, new InternetAddress("receiver@gmail.com"));
    
            transport.connect(SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);
            transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
            transport.close();
        }
    }

【问题讨论】:

  • 您遇到什么错误?
  • 线程“主”java.lang.RuntimeException 中的异常:无法编译的源代码 - 错误的符号类型:javax.mail.internet.MimeMessage.addRecipient
  • 这是一个编译错误。修复它。

标签: java email


【解决方案1】:

变化:

message.setRecipients(Message.RecipientType.TO,myToList); 

到:

message.addRecipients(Message.RecipientType.TO,myToList);

然后:

transport.sendMessage(message, message.getAllRecipients());

【讨论】:

  • myToList 出现错误,提示无法通过方法转换为地址?!???
  • 我不明白你的评论。
  • 废话...这很有趣,我刚刚在使用 set 时遇到错误,在我更改添加后错误消失了...现在我改回 set。它再次工作
【解决方案2】:

不是直接的答案,但这可以帮助您采取不同的方法。
我是公共电子邮件 jar 的粉丝:http://commons.apache.org/proper/commons-email/

您可以发送这样的电子邮件,这样阅读起来更直接。

Email email = new SimpleEmail();
email.setHostName("smtp.googlemail.com");
email.setSmtpPort(465);
email.setAuthenticator(new DefaultAuthenticator("username", "password"));
email.setSSLOnConnect(true);
email.setFrom("user@gmail.com");
email.setSubject("TestMail");
email.setMsg("This is a test mail ... :-)");
email.addTo("foo@bar.com");
email.send();

-瑞克

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-11
    • 2021-06-23
    • 1970-01-01
    • 2010-12-05
    • 1970-01-01
    • 2018-01-22
    相关资源
    最近更新 更多