public String host="smtp.163.com"; public String username="abcdefg"; public String password="abcdefg"; public String mail_head_name="this is head of this mail"; public String mail_head_value="this is head of this mail"; public String mail_to="xyz@163.com"; public String mail_from="abcdefg@163.com"; public String mail_subject="this is the subject of this test mail"; public String mail_body="this is the mail_body of this test mail"; //此段代码用来发送普通电子邮件 void jButton1_actionPerformed(ActionEvent e) { try { Properties props = new Properties();//获取系统环境 Authenticator auth = new Email_Autherticator();//进行邮件服务器用户认证
Address toAddress = new InternetAddress(mail_to);//设置邮件接收方的地址 message.addRecipient(Message.RecipientType.TO,toAddress); // Address ccAddress = new InternetAddress("firstsxm@hotmail.com");//设置邮件抄送者的地址 // message.addRecipient(Message.RecipientType.CC,ccAddress);
Transport.send(message);//发送邮件 /* // to get a specific instance from the session for your protocol.pass along the username and password // (blank if unnecessary).send the message,and close the connection; message.saveChanges(); Transport transport = session.getTransport("smtp"); transport.connect(host,username,password); transport.sendMessage(message,message.getAllRecipients()); transport.close(); */ System.out.println("send ok!"); } catch(Exception ex) { System.out.println("faild"+ex); } }