javaMail.docx_第1页
javaMail.docx_第2页
javaMail.docx_第3页
javaMail.docx_第4页
javaMail.docx_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

/www1/?action-category-catid-96 /Java/JavaMail/javamail HelloWork下面程序发送邮件消息,它需要三个命令行参数 SMTP Server From Email Address To Email Address 例如: java Example smtp.mailserver import java.util.Properties; import javax.mail.*; import ernet.*; public class MailExample public static void main(String args) throws Exception String host=args0; String from=args1; String to=args2; /Get system properties Properties props=System.getProperties(); /Setup mail server props.put(mail.smtp.host,host); /Get Session Session session =Session.getInstance(props,null); /Define message MimeMessage message=new MimeMessage(session); message.setFrom(new InternetAddress(from); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to); message.setSubject(Hello JavaMail); message.setText(Welcome to JavaMail); /send message Transport.send(message); javamail 发送附件的例子import java.util.Properties;import javax.mail.*;import ernet.*;import javax.activation.*;public class AttachExample public static void main (String args) throws Exception String host = args0; String from = args1; String to = args2; String fileAttachment = args3; / Get system properties Properties props = System.getProperties(); / Setup mail server props.put(mail.smtp.host, host); / Get session Session session = Session.getInstance(props, null); / Define message MimeMessage message = new MimeMessage(session); message.setFrom( new InternetAddress(from); message.addRecipient( Message.RecipientType.TO, new InternetAddress(to); message.setSubject( Hello JavaMail Attachment); / create the message part MimeBodyPart messageBodyPart = new MimeBodyPart(); /fill message messageBodyPart.setText(Hi); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); / Part two is attachment messageBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(fileAttachment); messageBodyPart.setDataHandler( new DataHandler(source); messageBodyPart.setFileName(fileAttachment); multipart.addBodyPart(messageBodyPart); / Put parts in message message.setContent(multipart); / Send the message Transport.send( message ); 使用javamail发信过程中的一些问题及解决方法今天在研究javamail发信的过程中,出现了一些小问题,现总结如下,以免后来者走些不必要的弯路,先把完整的能够正常运行的代码示例粘贴如下: 发邮件源代码: package com.hyq.test; import java.util.Properties; import javax.mail.*; import ernet.*; public class MailExample public static void main (String args) throws Exception String host = ; /发件人使用发邮件的电子信箱服务器 String from = 你自己的电子信箱; /发邮件的出发地(发件人的信箱) String to = 收件人信箱; /发邮件的目的地(收件人信箱) / Get system properties Properties props = System.getProperties(); / Setup mail server props.put(mail.smtp.host, host); / Get session props.put(mail.smtp.auth, true); /这样才能通过验证 MyAuthenticator myauth = new MyAuthenticator(你自己的电子信箱, 你自己的信箱密码); Session session = Session.getDefaultInstance(props, myauth); / session.setDebug(true); / Define message MimeMessage message = new MimeMessage(session); / Set the from address message.setFrom(new InternetAddress(from); / Set the to address message.addRecipient(Message.RecipientType.TO, new InternetAddress(to); / Set the subject message.setSubject(测试程序!); / Set the content message.setText(这是用java写的发送电子邮件的测试程序!); message.saveChanges(); Transport.send(message); 校验发信人权限的方法 package com.hyq.test; import javax.mail.PasswordAuthentication; class MyAuthenticator extends javax.mail.Authenticator private String strUser; private String strPwd; public MyAuthenticator(String user, String password) this.strUser = user; this.strPwd = password; protected PasswordAuthentication getPasswordAuthentication() return new PasswordAuthentication(strUser, strPwd); 注意:上面的事例仅为使用163信箱时发送电子邮件的方法,因为使用的host为:,如源代码中:String host = ; /发件人使用发邮件的电子信箱服务器,如果使用其它的电子邮件发送,就必须在其邮件服务器上查找相应的电子邮件服务器,例如搜狐就要使用,具体情况具体对待,都可以从所使用的邮件服务器上获得的。如果没有使用host ,也就是说,没有进行props.put(mail.smtp.host, host);设置,那么就会抛javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;的异常。当然了,如果你没有正确配置,这个异常仍然会被抛出的。 有些邮件服务系统是不需要验证发件人的授权的,所以可以很简单的使用 Session session = Session.getDefaultInstance(props, null); 而不必使用 props.put(mail.smtp.auth, true); MyAuthenticator myauth = new MyAuthenticator(你自己的电子信箱, 你自己的信箱密码); Session session = Session.getDefaultInstance(props, myauth); 就可以发送电子邮件了,这个多为一些企事业单位的内部电子信箱系统。 但是对于很多门户网站上的电邮系统,如:163,sohu,yahoo等等,如果仍然简单的这样使用就会抛 com.sun.mail.smtp.SMTPSendFailedException: 553 authentication is required,smtp8,wKjADxuAyCAfmnZE8BwtIA=.32705S2 at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388) at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959) at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583) at javax.mail.Transport.send0(Transport.java:169) at javax.mail.Transport.send(Transport.java:98) 这样的异常,要求你必须进行授权校验,它的目的就是阻止他人任意乱发邮件,也算是为了减少垃圾邮件的出现吧。这时候,我们就要使用 props.put(mail.smtp.auth, true); MyAuthenticator myauth = new MyAuthenticator(你自己的电子信箱, 你自己的信箱密码); Session session = Session.getDefaultInstance(props, myauth); 这里还有一个特别注意的事情:在你使用Session.getDefaultInstance时,一定要将 props.put(mail.smtp.auth, true); 置为true,它默认的是false,如果你没有做这一步,虽然你使用了Session.getDefaultInstance(props, myauth);,你自己也确实 MyAuthenticator myauth = new MyAuthenticator(你自己的电子信箱, 你自己的信箱密码);但是它仍然会抛出 com.sun.mail.smtp.SMTPSendFailedException: 553 authentication is required,smtp8,wKjADxJA2SBrm3ZEFv0gIA=.40815S2 at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388) at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959) at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583) at javax.mail.Transport.send0(Transport.java:169) at javax.mail.Transport.send(Transport.java:98) 这样的异常。我就在这一步费了好长时间,后来才发现了这个问题,很是郁闷。不过还好,总算解决了。 其实上面的做法只是比较简单的一种,也有很多其它的写法,如: Properties props = System.getProperties();可以使用 Properties props = new Properties();来代替。 Transport.send(message);可以使用下面的代码来代替 String username = 你的电子信箱用户名; String password = 你的电子信箱密码; message.saveChanges(); / implicit with send() Transport transport = session.getTransport(smtp); transport.connect(, username, password); transport.sendMessage(message, message.getAllRecipients(); transport.close(); 这种方法在同时发送多封电子邮件时比较有用。 还有一些具体的相关概念,可以查看相关的官方文档,在我查询资料时,发现了一篇文章写得相当仔细,可以加以参考:/resource/article/44/44101_JavaMail.html 另附上使用mons.mail进行发电子邮件的示例: import mons.mail.SimpleEmail; import mons.mail.*; public class TestCommon public TestCommon() public static void main(String args) SimpleEmail email = new SimpleEmail(); email.setHostName();/设置使用发电子邮件的邮件服务器 try email.addTo(收件人信箱); email.setAuthentication(发件人信箱,发件人信箱密码); email.setFrom(发件人信箱); email.setSubject(Test mons.mail message); email.setMsg(This is a simple test of commons-email); email.send(); catch (EmailException ex) ex.printStackTrace(); 发送三种类型的附件前面我们已学会了发送一般文本邮件和超文本邮件,今天我们将让大家学会编写三种类型的附件的邮件 发送程序.(注:撰写界面仍然用前面的)发送成功发送成功!去看看我的信箱*-*一下是完整可以运行的例子:第一个是一般信件sendMailOne(),第二个是带附件sendMailTwo(),package com;import javax.swing.SwingUtilities;import java.awt.BorderLayout;import javax.swing.JPanel;import javax.swing.JFrame;import java.awt.Dimension;import java.util.Properties; import javax.activation.DataHandler;import javax.activation.DataSource;import javax.activation.FileDataSource;import javax.mail.*; import ernet.*;import javax.swing.JButton;import java.awt.Rectangle; import javax.mail.PasswordAuthentication;public class JavaMail extends JFrame private static final long serialVersionUID = 1L;private JPanel jContentPane = null;private JButton jButton = null;private JButton jButton1 = null;/* * This method initializes jButton * * return javax.swing.JButton */private JButton getJButton() if (jButton = null) jButton = new JButton();jButton.setBounds(new Rectangle(91, 480, 142, 33);jButton.setText(发送一);jButton.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) System.out.println(actionPerformed(); / TODO Auto-generated Event stub actionPerformed()sendMailOne(););return jButton;/* * This method initializes jButton1 * * return javax.swing.JButton */private JButton getJButton1() if (jButton1 = null) jButton1 = new JButton();jButton1.setBounds(new Rectangle(462, 480, 128, 34);jButton1.setText(发送二);jButton1.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) System.out.println(actionPerformed(); / TODO Auto-generated Event stub actionPerformed()sendMailTwo(););return jButton1;/* * param args */public static void main(String args) / TODO 自动生成方法存根SwingUtilities.invokeLater(new Runnable() public void run() JavaMail thisClass = new JavaMail();thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);thisClass.setVisible(true););/* * This is the default constructor */public JavaMail() super();initialize();/* * This method initializes this * * return void */private void initialize() int width=700;int height=600;this.setSize(700, 600);this.setBounds(250, 100, 700, 600);this.setContentPane(getJContentPane();this.setTitle(发送邮件);/* * This method initializes jContentPane * * return javax.swing.JPanel */private JPanel getJContentPane() if (jContentPane = null) jContentPane = new JPanel();jContentPane.setLayout(null);jContentPane.add(getJButton(), null);jContentPane.add(getJButton1(), null);return jContentPane;public String sendMailOne() tryString host=; String from=;String to=106110454; MyAuthenticator myauth = new MyAuthenticator(, 77185201314);/String host=; /String from=106110454; /String to=;/MyAuthenticator myauth = new MyAuthenticator(106110454, 015989005206); System.out.println(Start Send Mail.);/Get system properties Properties props=System.getProperties(); /Setup mail server props.put(mail.smtp.host,host); props.put(mail.smtp.auth, true);/MyAuthenticator myauth = new MyAuthenticator(, 77185201314); /Get SessionSession session = Session.getDefaultInstance(props, myauth); /Session session =Session.getInstance(props,null); /Define message MimeMessage message=new MimeMessage(session); message.setFrom(new InternetAddress(from); message.addRecipient(Message.RecipientType.TO,new InternetAddress(to); message.setSubject(Hello JavaMail); message.setText(Welcome to JavaMail); message.saveChanges(); /send message Transport.send(message);System.out.println(End Send Mail.);catch(Exception e)e.printStackTrace();return null;public String sendMailTwo()try String fileAttachment =D:commonlog.html; String host=; String from=;String to=106110454; MyAuthenticator myauth = new MyAuthenticator(, 77185201314);/String host=; /String from=106110454; /String to=;/MyAuthenticator myauth = new MyAuthenticator(106110454, 015989005206); System.out.println(Start Send Mail.);/ Get system properties Properties props = System.getProperties();/ Setup mail server props.put(mail.smtp.host,host); props.put(mail.smtp.auth, true); /MyAuthenticator myauth = new MyAuthenticator(, 77185201314); /Get SessionSession session = Session.getDefaultInstance(props, myauth); /Session session =Session.getInstance(props,nul

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论