简体   繁体   English

使用smtp在Java中发送邮件

[英]Sending an mail in java using smtp

I am trying to send an email but i am getting following error: 我正在尝试发送电子邮件,但出现以下错误:

javax.mail.SendFailedException: Sending failed;
nested exception is: 
javax.mail.MessagingException: Could not connect to SMTP host: 10.17.1.65, port: 25

test the netstat command first whether the host is responding or not. 首先测试netstat命令,看看主机是否响应。 if it is yes then you need to check an email server/relay on host. 如果是,那么您需要检查主机上的电子邮件服务器/中继。 you can test it by this link http://www.mailradar.com/openrelay/ 您可以通过此链接http://www.mailradar.com/openrelay/对其进行测试

Here I am sharing a class for sending mail using Gmail SSL with multiple attachments. 在这里,我分享了一个用于使用带有多个附件的Gmail SSL发送邮件的类。 Just change the gmailUsername and gmailPass with actual gmail credentials. 只需使用实际的gmail凭据更改gmailUsername和gmailPass。 And also change the to and from address with your desired one. 并用您想要的地址更改往返地址。 Here we have used Gmail SSL smtp port for sending mail. 在这里,我们已使用Gmail SSL smtp端口发送邮件。 You can change it with your own mailing server details. 您可以使用自己的邮件服务器详细信息进行更改。

import java.util.*;

import javax.mail.*;

import javax.mail.internet.*;

import javax.activation.*;

/**

Author Mridul Chatterjee

 */

import java.util.Properties;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.PasswordAuthentication;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;



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″);

ArrayList fileNames = new ArrayList();

fileNames.add(”C:/Write.txt”);

fileNames.add(”C:/Write1.txt”);

fileNames.add(”C:/Write2.txt”);

fileNames.add(”C:/Write3.txt”);

fileNames.add(”C:/25148.jpg”);



Session session = Session.getDefaultInstance(props,

new javax.mail.Authenticator() {

protected PasswordAuthentication getPasswordAuthentication() {

return new PasswordAuthentication(”gmailUsername”,”gmailPass”);

}

});



try {



Message message = new MimeMessage(session);

message.setFrom(new InternetAddress(”from@no-spam.com”));

message.setRecipients(Message.RecipientType.TO,

InternetAddress.parse(”tomail@mail.com”));

message.setSubject(”Testing Subject”);

message.setText(”Dear Mail Crawler,” +

“nn No spam to my email, please!”);

     //  multipart.addBodyPart(messageBodyPart);



     //  DataSource source = new FileDataSource(filename);

    //   messageBodyPart.setDataHandler(new DataHandler(source));

    //   messageBodyPart.setFileName(filename);

       System.out.println(fileNames.size());

       Multipart multipart = new MimeMultipart();

       BodyPart messageBodyPart = new MimeBodyPart();

       for(int i=0;i            {

                System.out.println(fileNames.get(i));





                messageBodyPart = new MimeBodyPart();

                DataSource source = new FileDataSource((String)fileNames.get(i));

                messageBodyPart.setDataHandler(new DataHandler(source));

                messageBodyPart.setFileName((String)fileNames.get(i));

                multipart.addBodyPart(messageBodyPart);

                //message.setContent(multipart);

            }



       //multipart.addBodyPart(messageBodyPart);

       message.setContent(multipart);



Transport.send(message);



System.out.println(”Mail Sent Successfully….”);



} catch (MessagingException e) {

throw new RuntimeException(e);

}

}

}

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

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