如何从 Wicket 应用程序发送电子邮件?

发布于 2024-08-24 07:35:03 字数 29 浏览 2 评论 0原文

如何从 Wicket 应用程序发送电子邮件?

How can I send email from a Wicket application?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流星番茄 2024-08-31 07:35:03

使用 JavaMail API。

示例代码:

import javax.mail.*; 
 import javax.mail.internet.*; 
 import javax.activation.*; 
 import java.io.*; 
 import java.util.Properties; 
 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.mydomain.com"; 
             String from="[email protected]"; 
             String to = "[email protected]"; 
             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); 
         } 

     } 
 } 

Use the JavaMail API.

Example code:

import javax.mail.*; 
 import javax.mail.internet.*; 
 import javax.activation.*; 
 import java.io.*; 
 import java.util.Properties; 
 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.mydomain.com"; 
             String from="[email protected]"; 
             String to = "[email protected]"; 
             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); 
         } 

     } 
 } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文