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

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

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

        Gmail 作为 JavaMail SMTP 服务器

        时间:2023-10-13
        <tfoot id='uklP1'></tfoot>
        • <bdo id='uklP1'></bdo><ul id='uklP1'></ul>
          <i id='uklP1'><tr id='uklP1'><dt id='uklP1'><q id='uklP1'><span id='uklP1'><b id='uklP1'><form id='uklP1'><ins id='uklP1'></ins><ul id='uklP1'></ul><sub id='uklP1'></sub></form><legend id='uklP1'></legend><bdo id='uklP1'><pre id='uklP1'><center id='uklP1'></center></pre></bdo></b><th id='uklP1'></th></span></q></dt></tr></i><div id='uklP1'><tfoot id='uklP1'></tfoot><dl id='uklP1'><fieldset id='uklP1'></fieldset></dl></div>

                <tbody id='uklP1'></tbody>

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

                <legend id='uklP1'><style id='uklP1'><dir id='uklP1'><q id='uklP1'></q></dir></style></legend>
                • 本文介绍了Gmail 作为 JavaMail SMTP 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我一直在以 Gmail 作为主机使用 JavaMail API,并且大致了解如何使用它来发送电子邮件.但是有两行代码还是让我很困惑.

                  I have been working with the JavaMail API with Gmail as my host and have a general understanding of how it can be used to send emails. But there are two lines of code that still confuse me.

                  message.setFrom(new InternetAddress(USERNAME));
                  

                  API 说这用于设置此消息中的发件人"属性."但是,当我从代码中删除这一行并发送电子邮件时,电子邮件与该行存在时相比没有明显变化.我认为这对 Gmail 来说是有目的的,以防止垃圾邮件,这让我想知道在使用 Gmail 作为主机时是否有必要这样做.

                  API says that this is used to "Set the "From" attribute in this Message." But when I delete this line from the code and send the email, the email has no discernible changes from when the line is present. I've assumed this is purposeful on Gmail's part to prevent spam, which leaves me wondering if this is necessary at all when using Gmail as a host.

                  这也给我带来了麻烦.

                  props.put("mail.smtp.auth", "true");
                  

                  根据我收集到的信息,这表明主机是否需要身份验证,Gmail 会这样做.然而,将其设置为 false 似乎什么都不做,并且消息以与设置为 true 时相同的方式和时间发送.为什么会这样?

                  From what I have gathered, this indicates whether or not the host requires authentication, which Gmail does. Yet setting it to false seems to do nothing and the message is sent in the same manner and time as with it set as true. Why is this the case?

                  感谢您的帮助.如果有帮助,这是我的所有代码.

                  Thanks for any help. Here is all my code if it helps.

                  import javax.mail.internet.InternetAddress;
                  import javax.mail.internet.MimeMessage;
                  import javax.mail.Message;
                  import javax.mail.MessagingException;
                  import javax.mail.Session;
                  import javax.mail.Transport;
                  import java.util.Properties;
                  public class SendEmail
                  {
                      private String msg;
                      private String className;
                      private final String USERNAME = "email@gmail.com";
                      private final String PASSWORD = "password";
                      private final String HOST = "smtp.gmail.com";
                      public SendEmail(String email, String text, String title)
                      {
                          String to = email;
                          Properties props = new Properties();
                          props.put("mail.smtp.auth", "true");
                          props.put("mail.smtp.starttls.enable", "true");
                          props.put("mail.smtp.host", HOST);
                          props.put("mail.smtp.port", "587");
                          Session session = Session.getInstance(props, null);
                          try
                          {
                              Message message = new MimeMessage(session);
                              message.setFrom(new InternetAddress(USERNAME));
                              message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
                              message.setSubject(title);
                              message.setText(text);
                  
                              Transport.send(message, USERNAME, PASSWORD);
                              msg = "Email Successfully Sent";
                          }
                          catch(Exception ex)
                          {
                              msg = ex.getClass().getName();
                          }
                      }
                  }
                  

                  推荐答案

                  第一个,

                  message.setFrom(new InternetAddress(USERNAME));
                  

                  正在使用 RFC 5321 - 第 3.3 节邮件事务 MAIL 命令(包括 FROM).同样,mail.smtp.auth

                  is using the RFC 5321 - Section 3.3 Mail Transactions MAIL command (which includes FROM). Similarly, the mail.smtp.auth appears to be optional in

                  props.put("mail.smtp.auth", "true");`
                  

                  因为库假定您在指定 USERNAMEPASSWORD 时要使用 mail.smtp.auth

                  because the library assumes you want to use mail.smtp.auth when you specify a USERNAME and PASSWORD at

                  Transport.send(message, USERNAME, PASSWORD);
                  

                  Transport.send(Message, String, String) Javadoc 说(部分)

                  the Transport.send(Message, String, String) Javadoc says (in part)

                  使用指定的用户名和密码对邮件服务器进行身份验证.

                  Use the specified user name and password to authenticate to the mail server.

                  这篇关于Gmail 作为 JavaMail SMTP 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Java Mail:在没有 SSL 的端口 25 上发送电子邮件时出现 SSLHandshakeException 下一篇:使用 Apache Commons 电子邮件库在 Java 中发送电子邮件

                  相关文章

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

                        <bdo id='wTWuF'></bdo><ul id='wTWuF'></ul>
                      <legend id='wTWuF'><style id='wTWuF'><dir id='wTWuF'><q id='wTWuF'></q></dir></style></legend>

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