Java发送邮件 [英] Java send mail

查看:82
本文介绍了Java发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class MailClient
{
  public void sendMail(String mailServer, String from, String to,
		       String subject, String messageBody,
		       String[] attachments) throws
		       MessagingException, AddressException
  {
    // Setup mail server
    Properties props = System.getProperties();
    props.put("mail.smtp.host", mailServer);

    // Get a mail session
    Session session = Session.getDefaultInstance(props, null);

    // Define a new mail message
    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
    message.setSubject(subject);
    // Create a message part to represent the body text
    BodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setText(messageBody);

    //use a MimeMultipart as we need to handle the file attachments
    Multipart multipart = new MimeMultipart();
    //add the message body to the mime message
    multipart.addBodyPart(messageBodyPart);

    // add any file attachments to the message
    addAtachments(attachments, multipart);

    // Put all message parts in the message
    message.setContent(multipart);

    // Send the message
    Transport.send(message);
  }
  
  protected void addAtachments(String[] attachments, Multipart multipart) throws MessagingException, AddressException
  {
    for(int i = 0; i<= attachments.length -1; i++)
    {
      String filename = attachments[i];
      MimeBodyPart attachmentBodyPart = new MimeBodyPart();

      //use a JAF FileDataSource as it does MIME type detection
      DataSource source = new FileDataSource(filename);
      attachmentBodyPart.setDataHandler(new DataHandler(source));
      //assume that the filename you want to send is the same as the
      //actual file name - could alter this to remove the file path
      attachmentBodyPart.setFileName(filename);

      //add the attachment
      multipart.addBodyPart(attachmentBodyPart);
    }
  }

  public static void main(String[] args)
  {
    try
    {
      MailClient client = new MailClient();
      String server="pop3.yahoo.com";
      String from="email1@yahoo.com";
      String to = "email2@yahoo.com";
      String subject="Test";
      String message="Testing";
      String[] filenames = {"c:/somefile.txt"};
      client.sendMail(server,from,to,subject,message,filenames);
    }
    catch(Exception e)
    {
      e.printStackTrace(System.out);
    }
  }
}



错误:



ERROR:

<br />
javax.mail.MessagingException: Could not connect to SMTP host: pop3.yahoo.com, port: 25;<br />
  nested exception is:<br />
	java.net.ConnectException: Connection timed out: connect<br />
	at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1227)<br />
	at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:322)<br />
	at javax.mail.Service.connect(Service.java:236)<br />
	at javax.mail.Service.connect(Service.java:137)<br />
	at javax.mail.Service.connect(Service.java:86)<br />
	at javax.mail.Transport.send0(Transport.java:150)<br />
	at javax.mail.Transport.send(Transport.java:80)<br />
	at pack.MailClient.sendMail(MailClient.java:57)<br />
	at pack.MailClient.main(MailClient.java:94)<br />

推荐答案

据我记得,雅虎没有免费提供对SMTP主机的直接访问.你得付钱.不像谷歌.通常,我将gmail帐户用于此类实现.
As far as I remember Yahoo not provide direct access to SMTP host, free. You have to pay. Not like google. Normally I use gmail account for these kind of implementation.


您能显示一下MailClient.java中第94行的内容吗?我的意思是全部吗?
Can you show what you have on line 94 in MailClient.java. I mean is this all?


您是什么意思?在第94行中,它调用函数"sendMail",然后该函数发送邮件
what do you mean? in line 94 its calls a function "sendMail" and that function send mail


这篇关于Java发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆