简体   繁体   English

关于从java程序发送email的问题

[英]A question about sending email from java program

I need to send email from java program.我需要从 java 程序发送 email。 I am first trying to understand basics.我首先尝试了解基础知识。 I found a snippet at:我在以下位置找到了一个片段:

https://www.javatpoint.com/example-of-sending-email-using-java-mail-api https://www.javatpoint.com/example-of-sending-email-using-java-mail-api

import java.util.*;  
import javax.mail.*;  
import javax.mail.internet.*;  
import javax.activation.*;  
  
public class SendEmail  
{  
 public static void main(String [] args){  
      String to = "sonoojaiswal1988@gmail.com";//change accordingly  
      String from = "sonoojaiswal1987@gmail.com";change accordingly  
      String host = "localhost";//or IP address  
  
     //Get the session object  
      Properties properties = System.getProperties();  
      properties.setProperty("mail.smtp.host", host);  
      Session session = Session.getDefaultInstance(properties);  
  
     //compose the message  
      try{  
         MimeMessage message = new MimeMessage(session);  
         message.setFrom(new InternetAddress(from));  
         message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));  
         message.setSubject("Ping");  
         message.setText("Hello, this is example of sending email  ");  
  
         // Send message  
         Transport.send(message);  
         System.out.println("message sent successfully....");  
  
      }catch (MessagingException mex) {mex.printStackTrace();}  
   }  
}  

My question is that as per the code, it looks like anyone can use any sender email address string and send infinite emails to any receiver email address.我的问题是,根据代码,看起来任何人都可以使用任何发件人 email 地址字符串并向任何收件人 email 地址发送无限电子邮件。 I am missing something in my understanding, which will prevent such scenario to happen.我在我的理解中遗漏了一些东西,这将防止这种情况发生。 Please help.请帮忙。

I understand that this is not a programming question, but guess, it will not take too much time to answer this basic question and don't know any other equally active forum.我知道这不是一个编程问题,但猜想,回答这个基本问题不会花费太多时间,而且不知道任何其他同样活跃的论坛。

This example works for servers which don't need authentication.此示例适用于不需要身份验证的服务器。 And this is usually not applicable to the smtp servers used in production.这通常不适用于生产中使用的 smtp 服务器。 Such servers are used mostly for testing purposes where they are not exposed over the internet.此类服务器主要用于不通过 Internet 公开的测试目的。 Hence, although its possible to send infinite number of mails as mentioned by you, no one would be interested in doing the same.因此,尽管可以发送您提到的无限数量的邮件,但没有人有兴趣这样做。

For the servers where authentication is necessary, credentials need to be provided.对于需要身份验证的服务器,需要提供凭据。 And this is explained in detail in the blog mentioned by you.这在您提到的博客中进行了详细说明。

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

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