【问题标题】:Sending an mail in java using smtp使用 smtp 在 java 中发送邮件
【发布时间】:2012-11-13 09:43:45
【问题描述】:

我正在尝试发送电子邮件,但出现以下错误:

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

【问题讨论】:

标签: java


【解决方案1】:

首先测试 netstat 命令是否主机响应。如果是,那么您需要检查主机上的电子邮件服务器/中继。你可以通过这个链接测试它http://www.mailradar.com/openrelay/

【讨论】:

    【解决方案2】:

    在这里,我分享了一个使用 Gmail SSL 发送邮件的课程,其中包含多个附件。 只需使用实际的 gmail 凭据更改 gmailUsername 和 gmailPass。并使用您想要的地址更改往返地址。 在这里,我们使用了 Gmail SSL smtp 端口来发送邮件。您可以使用自己的邮件服务器详细信息对其进行更改。

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

    【讨论】:

      猜你喜欢
      • 2017-03-18
      • 2012-08-17
      • 2016-01-13
      • 2010-12-06
      • 2017-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多