简体   繁体   English

为什么GMail接受未经身份验证的邮件发送?

[英]Why GMail is accepting to send mail without Authentication?

public class SendMail {

  private class SMTPAuthenticator extends javax.mail.Authenticator 
  {

    @Override
    public PasswordAuthentication getPasswordAuthentication() {
      return new PasswordAuthentication("userID", "pwd");
    }
  }

   public void sendMail() throws Exception {
   String strFromIds = "xyz@gmail.com";
   String strToIds = "xyz@domain.com";
   String strSubject = "Sample Mail Subject.";
   String strContent = "Sample Mail Content";
   Properties objProperties = System.getProperties();
   objProperties.put("mail.smtp.host", "<smtp host name>");
   objProperties.put("mail.smtp.port", "25");
   objProperties.put("mail.transport.protocol", "smtp");
   objProperties.put("mail.smtp.submitter", "<user id>");
   objProperties.put("mail.smtp.auth", true);
   objProperties.put("mail.debug", "true");
   Session objSMTPSession = Session.getDefaultInstance(objProperties, new  
                                                     SMTPAuthenticator());

   Message objMessage = new MimeMessage(objSMTPSession);
   objMessage.setFrom(new InternetAddress(strFromIds));
   InternetAddress[] objToAddress = new InternetAddress[1];     
   objToAddress[0] = new InternetAddress(strToIds);
   objMessage.setRecipients(Message.RecipientType.TO, objToAddress);

   objMessage.setSubject(strSubject);

   Multipart objMultiPart = new MimeMultipart();
   MimeBodyPart objBodyPart = new MimeBodyPart();

   objBodyPart.setText(strContent);
   objMultiPart.addBodyPart(objBodyPart);

   objMessage.setContent(objMultiPart);

   Date objSentDate = new Date();
   objMessage.setSentDate(objSentDate);
   Transport.send(objMessage);
    objMessage = null;
 }

 public static void main(String[] args) {
try {
    new SendMail().sendMail();
} catch (Exception ex) {
    System.out.println("Exception in main :: " + ex);
    }
 }
}

By using the above code,i am able to send a mail to gmail user with the from address of GMail mail id(eg:xyz@gmail.com), without giving authentication details of gmail id, 通过使用上述代码,我可以使用Gmail邮件ID的发件人地址(例如:xyz@gmail.com)向gmail用户发送邮件,而无需提供gmail id的身份验证详细信息,

here i gave my smtp ( company mail server ) server host name, and userid and pwd of my company mail server( which is given as smtp host)... 在这里,我给了我的smtp(公司邮件服务器)服务器主机名,以及我公司的邮件服务器(以smtp主机的形式给出)的用户名和密码。

With these, i am sending mail as GMail user,, 有了这些,我以GMail用户的身份发送邮件,

But why GMAIL is accepting this type of mails. 但是,为什么GMAIL接受这种类型的邮件。

You've discovered why there is spam. 您已经发现了为什么存在垃圾邮件。 :-) :-)

You're sending the message through your company's mail server. 您正在通过公司的邮件服务器发送邮件。 Your company's mail server doesn't appear to be checking whether the From address you use is valid for your mail server, so it's letting you use your Gmail address instead of your company address. 您公司的邮件服务器似乎没有检查您使用的“发件人”地址是否对您的邮件服务器有效,因此可以让您使用Gmail地址而不是公司地址。 No, it doesn't check with Gmail to find out if it's ok. 不,它不会与Gmail核对是否可以。

Gmail cannot sending mail without any authentication. 没有任何身份验证,Gmail无法发送邮件。

You can't Authenticate with wrong credentials. 您不能使用错误的凭据进行身份验证。 In another words if you have password (and gmail requires one) you can't log in without sending your password so you won't be able to send anything. 换句话说,如果您有密码(而gmail需要一个密码),则无法在不发送密码的情况下登录,因此您将无法发送任何内容。

In general, you can, sure. 一般来说,可以。 In your concrete example code you are using GMail which does not allow anonymous sending. 在您的具体示例代码中,您正在使用不允许匿名发送的GMail。

From their references: 从他们的参考:

smtp.gmail.com (use authentication) Use Authentication: Yes Port for TLS/STARTTLS: 587 Port for SSL: 465 smtp.gmail.com(使用身份验证)使用身份验证:是TLS / STARTTLS的端口:587 SSL的端口:465

An additional comment regarding your catch clause: 关于catch子句的其他注释:

In my opinion you are heavily misusing the exception idea. 我认为您正在严重滥用异常主意。 A better aproach would be something like: 更好的方法是:

catch(Exception x)
 {
var s = x.Message;
if ( x.InnerException!=null )
{
    s += Environment.NewLine + x.InnerException.Message;
}

MessageBox.Show(s);
}

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

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