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

  1. <legend id='Okzfx'><style id='Okzfx'><dir id='Okzfx'><q id='Okzfx'></q></dir></style></legend>

    1. <tfoot id='Okzfx'></tfoot>
      • <bdo id='Okzfx'></bdo><ul id='Okzfx'></ul>

      <small id='Okzfx'></small><noframes id='Okzfx'>

      在 JAVA 中通过 gmail smtp 服务器发送电子邮件

      Sending Email via gmail smtp server in JAVA(在 JAVA 中通过 gmail smtp 服务器发送电子邮件)

        <tbody id='ibVPO'></tbody>

          <tfoot id='ibVPO'></tfoot>

              <small id='ibVPO'></small><noframes id='ibVPO'>

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

              <legend id='ibVPO'><style id='ibVPO'><dir id='ibVPO'><q id='ibVPO'></q></dir></style></legend>
                <bdo id='ibVPO'></bdo><ul id='ibVPO'></ul>
                本文介绍了在 JAVA 中通过 gmail smtp 服务器发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                这段代码有什么问题?不知何故,它在 Transport.send(message); 行进入无限循环,没有错误消息,没有异常,只是可能无限循环(我不知道,因为我不知道等待超过 5-10 分钟)

                What is the problem with this code? Somehow it is getting in to an infinity loop at the line Transport.send(message); line, no error message, no exception, just maybe infinite loop (i don't know because i don't wait more than 5-10minute)

                final String username = "<mail_name>";
                final String password = "<password>";
                
                Properties props = new Properties();
                props.put("mail.smtp.auth", "true");
                props.put("mail.smtp.starttls.enable", "true");
                props.put("mail.smtp.host", "smtp.gmail.com");
                props.put("mail.smtp.port", "465");
                
                Session session = Session.getInstance(props,
                        new javax.mail.Authenticator() {
                            protected PasswordAuthentication getPasswordAuthentication() {
                                return new PasswordAuthentication(username, password);
                            }
                        });
                
                try {
                
                    Message message = new MimeMessage(session);
                    message.setFrom(new InternetAddress("<mail_from>@gmail.com"));
                    message.setRecipients(Message.RecipientType.TO,
                            InternetAddress.parse("<mail_to>@gmail.com"));
                    message.setSubject("Test Subject");
                    message.setText("Test");
                
                    Transport.send(message);
                
                    System.out.println("Done");
                
                } catch (MessagingException e) {
                    throw new RuntimeException(e);
                }
                

                推荐答案

                我在这里做一些改变,对我来说很好用:

                Here I am giving some changes, that work fine for me:

                Session session = Session.getInstance(props,null);
                

                你像你一样实例化消息对象.最后:

                You instantiate message object as you did. And finally:

                Transport transport = session.getTransport("smtp");
                String mfrom = "yourGmailUsernameWithout@"// example laabidiraissi 
                transport.connect("smtp.gmail.com", mfrom, "thepassword");
                transport.sendMessage(message, message.getAllRecipients());
                

                编辑,请你帮我一个忙,复制/粘贴并尝试这个示例并显示它显示的内容:

                Edit, would you please give me a favor and copy/paste and try this example and show what it displays:

                package com.test;
                
                import java.util.Properties;
                
                import javax.mail.BodyPart;
                import javax.mail.Message;
                import javax.mail.MessagingException;
                import javax.mail.Multipart;
                import javax.mail.Session;
                import javax.mail.Transport;
                import javax.mail.internet.AddressException;
                import javax.mail.internet.InternetAddress;
                import javax.mail.internet.MimeBodyPart;
                import javax.mail.internet.MimeMessage;
                import javax.mail.internet.MimeMultipart;
                
                import org.junit.Test;
                
                public class EmailService {
                
                @Test
                public void test(){
                    Properties props = System.getProperties();
                    props.put("mail.smtp.starttls.enable", true); // added this line
                    props.put("mail.smtp.host", "smtp.gmail.com");
                    props.put("mail.smtp.user", "username");
                    props.put("mail.smtp.password", "password");
                    props.put("mail.smtp.port", "587");
                    props.put("mail.smtp.auth", true);
                
                
                
                    Session session = Session.getInstance(props,null);
                    MimeMessage message = new MimeMessage(session);
                
                    System.out.println("Port: "+session.getProperty("mail.smtp.port"));
                
                    // Create the email addresses involved
                    try {
                        InternetAddress from = new InternetAddress("username");
                        message.setSubject("Yes we can");
                        message.setFrom(from);
                        message.addRecipients(Message.RecipientType.TO, InternetAddress.parse("receivermail"));
                
                        // Create a multi-part to combine the parts
                        Multipart multipart = new MimeMultipart("alternative");
                
                        // Create your text message part
                        BodyPart messageBodyPart = new MimeBodyPart();
                        messageBodyPart.setText("some text to send");
                
                        // Add the text part to the multipart
                        multipart.addBodyPart(messageBodyPart);
                
                        // Create the html part
                        messageBodyPart = new MimeBodyPart();
                        String htmlMessage = "Our html text";
                        messageBodyPart.setContent(htmlMessage, "text/html");
                
                
                        // Add html part to multi part
                        multipart.addBodyPart(messageBodyPart);
                
                        // Associate multi-part with message
                        message.setContent(multipart);
                
                        // Send message
                        Transport transport = session.getTransport("smtp");
                        transport.connect("smtp.gmail.com", "username", "password");
                        System.out.println("Transport: "+transport.toString());
                        transport.sendMessage(message, message.getAllRecipients());
                
                
                    } catch (AddressException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (MessagingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                }
                

                这篇关于在 JAVA 中通过 gmail smtp 服务器发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Compiling C++ for the JVM(为 JVM 编译 C++)
                Compile to java bytecode (without using Java)(编译成java字节码(不使用Java))
                How to drive C#, C++ or Java compiler to compute 1+2+3+...+1000 at compile time?(如何在编译时驱动 C#、C++ 或 Java 编译器计算 1+2+3+...+1000?)
                Java ClassLoader: load same class twice(Java ClassLoader:两次加载相同的类)
                How to debug .class files in ECLIPSE?(如何在 ECLIPSE 中调试 .class 文件?)
                Java quot;The blank final field may not have been initializedquot; Anonymous Interface vs Lambda Expression(Java“可能尚未初始化空白的最终字段匿名接口与 Lambda 表达式)

                  <legend id='1jpxZ'><style id='1jpxZ'><dir id='1jpxZ'><q id='1jpxZ'></q></dir></style></legend>
                    • <tfoot id='1jpxZ'></tfoot>
                      • <bdo id='1jpxZ'></bdo><ul id='1jpxZ'></ul>

                        • <small id='1jpxZ'></small><noframes id='1jpxZ'>

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