繁体   English   中英

在Java中发送不带gmail身份验证的电子邮件

[英]send email w/o gmail authentication in java

嗨,我必须用Java发送电子邮件。

下面为我​​成功地工作了。

          public class SendMail {

             public static void main(String[] args) {                   
                 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 session = Session.getDefaultInstance(props,
                                   new javax.mail.Authenticator() {
                                     protected PasswordAuthentication getPasswordAuthentication() {
                                        return new PasswordAuthentication("xxxx@gmail.com","xxxxx!1");
                                     }
                                   });

                  try {
                        Message message = new MimeMessage(session);
                        message.setFrom(new InternetAddress("krishnaveni.veeman@gmail.com"));
                        message.setRecipients(Message.RecipientType.TO,
                                      InternetAddress.parse("mercy.krishnaveni@gmail.com"));
                        message.setSubject("Testing Subject");
                        message.setText("Dear Mail Crawler," +
                                      "\n\n No spam to my email, please!");
                        Transport.send(message);
                        System.out.println("Done"); 
                      } catch (MessagingException e) {
                          throw new RuntimeException(e);
                      }
              }

           }

在这里,我提到了gmail用户名和密码。 但是我必须在不使用用户名和密码的情况下发送电子邮件。 我该如何发展。 请帮我。

创建一个mail.properties文件,并将用户名和密码放入该文件中。 使用属性从代码中的该文件中检索数据。

此链接对于加载Properties对象可能有用

http://www.dzone.com/snippets/loading-property-file

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM