<i id='soD2W'><tr id='soD2W'><dt id='soD2W'><q id='soD2W'><span id='soD2W'><b id='soD2W'><form id='soD2W'><ins id='soD2W'></ins><ul id='soD2W'></ul><sub id='soD2W'></sub></form><legend id='soD2W'></legend><bdo id='soD2W'><pre id='soD2W'><center id='soD2W'></center></pre></bdo></b><th id='soD2W'></th></span></q></dt></tr></i><div id='soD2W'><tfoot id='soD2W'></tfoot><dl id='soD2W'><fieldset id='soD2W'></fieldset></dl></div>
  1. <tfoot id='soD2W'></tfoot>

  2. <small id='soD2W'></small><noframes id='soD2W'>

      <bdo id='soD2W'></bdo><ul id='soD2W'></ul>

      <legend id='soD2W'><style id='soD2W'><dir id='soD2W'><q id='soD2W'></q></dir></style></legend>
    1. 使用 java 和 gmail 发送邮件

      时间:2023-10-13
        <tbody id='WtdCa'></tbody>

            <legend id='WtdCa'><style id='WtdCa'><dir id='WtdCa'><q id='WtdCa'></q></dir></style></legend>

              <tfoot id='WtdCa'></tfoot>
                <bdo id='WtdCa'></bdo><ul id='WtdCa'></ul>
              • <small id='WtdCa'></small><noframes id='WtdCa'>

                <i id='WtdCa'><tr id='WtdCa'><dt id='WtdCa'><q id='WtdCa'><span id='WtdCa'><b id='WtdCa'><form id='WtdCa'><ins id='WtdCa'></ins><ul id='WtdCa'></ul><sub id='WtdCa'></sub></form><legend id='WtdCa'></legend><bdo id='WtdCa'><pre id='WtdCa'><center id='WtdCa'></center></pre></bdo></b><th id='WtdCa'></th></span></q></dt></tr></i><div id='WtdCa'><tfoot id='WtdCa'></tfoot><dl id='WtdCa'><fieldset id='WtdCa'></fieldset></dl></div>
              • 本文介绍了使用 java 和 gmail 发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想发送一封带有日历附件 javaxmail 的电子邮件,我创建了这个类:

                I wanto to send a e-mail with an calendar attachment javaxmail and I created this class:

                public void sendEmail(String to, Calendar calendar) {
                    try {
                        String  d_uname = "myaccount@gmail.com";
                        String    d_password = "mypassword";
                        String d_host = "smtp.gmail.com";
                        String  d_port  = "587";//465,587
                
                        String from = "antonitocea@gmail.com";
                
                        String subject = "Subject";
                        String bodyText = "Body";
                
                        Properties prop = new Properties();
                
                        //prop.setProperty("mail.smtp.auth", "true");
                        prop.setProperty("mail.smtp.host", "smtp.gmail.com");
                        prop.setProperty("mail.smtp.protocol", "smtps");
                        prop.setProperty("mail.smtp.starttls.enable", "true");
                        prop.setProperty("mail.smtp.ssl.enable", "true");
                        prop.setProperty("mail.smtp.port",d_port);
                        prop.setProperty("mail.smtp.user", d_uname);
                
                
                        Session session = Session.getDefaultInstance(prop, null);
                        // Define message
                        session.setDebug(true);
                        MimeMessage message = new MimeMessage(session);
                        message.addHeaderLine("method=REQUEST");
                        message.addHeaderLine("charset=UTF-8");
                        message.addHeaderLine("component=VEVENT");
                
                
                        message.setFrom(new InternetAddress(from));
                        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
                        message.setSubject("Outlook Meeting Request Using JavaMail");
                
                        StringBuffer sb = new StringBuffer();
                
                        StringBuffer buffer = sb.append(calendar.toString());
                
                        // Create the message part 
                        BodyPart messageBodyPart = new MimeBodyPart();
                
                        // Fill the message 
                        messageBodyPart.setHeader("Content-Class", "urn:content-classes:calendarmessage");
                        messageBodyPart.setHeader("Content-ID","calendar_message");
                        messageBodyPart.setDataHandler(new DataHandler(
                                new ByteArrayDataSource(buffer.toString(), "text/calendar")));//very important
                
                
                        // Create a Multipart 
                        Multipart multipart = new MimeMultipart();
                
                        // Add part one 
                        multipart.addBodyPart(messageBodyPart);
                
                        Transport t = session.getTransport("smtp");
                        t.connect(d_host, 587, d_uname, d_password);
                
                
                        // Put parts in message 
                        message.setContent(multipart);
                
                        // send message 
                        t.send(message);
                    } catch (MessagingException me) {
                        me.printStackTrace();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
                

                看起来没问题,但是,当我尝试发送时,控制台显示此错误:

                It seems ok, but, when I try to send, the console shows this error:

                DEBUG: setDebug: JavaMail version 1.4ea
                DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
                DEBUG SMTP: useEhlo true, useAuth true
                DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
                javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587;
                  nested exception is:
                    java.net.SocketException: Permission denied: connect
                

                有人可以帮助我吗?提前致谢!!

                Can someone help me? Thanks in advance!!

                推荐答案

                使用下面的代码.

                public void sendTemplateEmail() {
                
                        Properties props = new Properties();  
                        props.put("mail.smtp.host", "smtp.gmail.com");  
                        props.put("mail.smtp.auth", "true");  
                        props.put("mail.debug", "true");  
                        props.put("mail.smtp.port", 25);  
                        props.put("mail.smtp.socketFactory.port", 25);  
                        props.put("mail.smtp.starttls.enable", "true");
                        props.put("mail.transport.protocol", "smtp");
                        Session mailSession = null;
                
                        mailSession = Session.getInstance(props,  
                                new javax.mail.Authenticator() {  
                            protected PasswordAuthentication getPasswordAuthentication() {  
                                return new PasswordAuthentication("<Gmail User Name>", "<Gmail Password>");  
                            }  
                        });  
                
                
                        try {
                
                            Transport transport = mailSession.getTransport();
                
                            MimeMessage message = new MimeMessage(mailSession);
                
                            message.setSubject("Sample Subject");
                            message.setFrom(new InternetAddress("Sample@sample.com"));
                            String []to = new String[]{"Sample2gmail.com"};
                            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to[0]));
                            String body = "Sample text";
                            message.setContent(body,"text/html");
                            transport.connect();
                
                            transport.sendMessage(message,message.getRecipients(Message.RecipientType.TO));
                            transport.close();
                        } catch (Exception exception) {
                
                        }
                    }
                

                替换上面代码中的所有 from 和 to 电子邮件地址以及 and.

                Replace all the from and to email addresses and the and in the above code.

                这篇关于使用 java 和 gmail 发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Java 字节码操作库建议 下一篇:使用 commons-email-1.3 发送电子邮件时出错

                相关文章

                  <bdo id='xXi97'></bdo><ul id='xXi97'></ul>

                <legend id='xXi97'><style id='xXi97'><dir id='xXi97'><q id='xXi97'></q></dir></style></legend>
                  <i id='xXi97'><tr id='xXi97'><dt id='xXi97'><q id='xXi97'><span id='xXi97'><b id='xXi97'><form id='xXi97'><ins id='xXi97'></ins><ul id='xXi97'></ul><sub id='xXi97'></sub></form><legend id='xXi97'></legend><bdo id='xXi97'><pre id='xXi97'><center id='xXi97'></center></pre></bdo></b><th id='xXi97'></th></span></q></dt></tr></i><div id='xXi97'><tfoot id='xXi97'></tfoot><dl id='xXi97'><fieldset id='xXi97'></fieldset></dl></div>

                  1. <small id='xXi97'></small><noframes id='xXi97'>

                  2. <tfoot id='xXi97'></tfoot>