当前位置: 代码迷 >> Java Web开发 >> [开源]Struts中JavaMail的实际应用,本人自己写的哦~
  详细解决方案

[开源]Struts中JavaMail的实际应用,本人自己写的哦~

热度:558   发布时间:2005-12-14 14:57:00.0
[开源]Struts中JavaMail的实际应用,本人自己写的哦~

注:是本人完成的一个系统的一部分.
DB是postgresql,
需要自己配置一下MailServer(TOMCAT那本大厚书里带的光盘上有个试用版的,30天)
还需要几个jar包,回头找找,再传上来~
由于开发时间较早,没来得及加注释,

======================
package utils;
import java.util.*;
import javax.mail.*;
import java.io.*;
import java.util.*;
import javax.mail.internet.*;
import javax.activation.*;
import business.*;
import business.postgresql.*;

public class DTSMail {

public static void sendMail (String from,
String to,
String subject,
String body) {
try {
Properties props = new Properties();
// 路径自己配置哦~
InputStream is = new FileInputStream("C:/Tomcat 5.0/webapps/DTS/WEB-INF/classes/utils/JavaMail.properties");
props.load(is);
String host = (String)props.get("smtphost");
String name = (String)props.get("username");
String pwd = (String)props.get("password");
props.put("mail.smtp.host", "localhost");
props.put("mail.smtp.auth","true");
Session ssn = Session.getInstance(props, null);

MimeMessage message = new MimeMessage(ssn);
InternetAddress fromAddress = new InternetAddress(from);
message.setFrom(fromAddress);
InternetAddress toAddress = new InternetAddress(to);
message.addRecipient(Message.RecipientType.TO, toAddress);
message.setSubject(subject);
message.setText(body);

Transport transport = ssn.getTransport("smtp");
transport.connect(host,name,pwd);
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
transport.send(message);
transport.close();

} catch(Exception m) {
System.out.println(m.toString());
}
}
}
============================
JavaMail.properties里面自己写:

smtphost = localhost
username = david
password = 12345
============================
JavaMailProperties.java :是来读JavaMail.properties里面内容的


import java.io.*;
import java.util.*;

class JavaMailProperties {
public static void main(String[] args) throws Exception {
Properties javamail = new Properties();
InputStream in = new FileInputStream("JavaMail.properties");
javamail.load(in);
System.out.println(javamail.get("smtphost"));
System.out.println(javamail.get("username"));
System.out.println(javamail.get("password"));
}
}
=========================================
以上三个文件放到同级目录下,本人编译后放在了WEB-INF\classes\utils
(utils与action,business,entity并列)


=========================================
另外,需要在JSP上BUTTON所调用的action里加上
//MAIL SERVER 自己配置
String from = "DTS@mydomain.com";
// DAO里的方法需要自己动手拉~我的是根据姓名取得email,其实可以直接写进去
String email = userDAO.getEmailByUsername(assignTo);
String to = email;
//MAIL 的标题(写死的)
String subject = "HELLO~!";
//MAIL的内容(写死的)
String body = "\hi~BABY.";


DTSMail.sendMail(from, to, subject, body);

[此贴子已经被作者于2005-12-14 15:11:32编辑过]

搜索更多相关主题的帖子: import  JavaMail  Struts  java  util  

----------------解决方案--------------------------------------------------------

我帮你顶一下,好帖子啊!哈哈!继续努力啊!
下面的是我弄的,大家给点意见吧!弄的不好,请指教!谢谢!(我的cc的那个部分没启用,注释了。cc是抄送的意思)

import java.util.*;

import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;

/**
* @author Jack Yang
* @CurrentDate 2006-2-6
* @CurrntTime 17:09:55 sendEmaiTest
*
*/
public class SendEmailT
{

/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
try
{
String smtpServer = "smtp的服务器地址,可以是ip,或者是域名";
String to = "发送给谁";
String from = "谁发的(可以是假的,但是域名要对,就是@后面的那一些啊!)";
String subject = "邮件主题";
String body = "邮件内容";
String attach = "邮件附件";
send(smtpServer, to, from, subject, body, attach);
}
catch (Exception ex)
{
System.out.println("Error");
}
}

/**
*
* "send" method to send the message.
*
*/

public static void send(String smtpServer, String to, String from,
String subject, String body, String attach)
{
try
{
Properties props = System.getProperties();

// -- Attaching to default Session, or we could start a new one --
props.put("mail.smtp.host", smtpServer);
Session session = Session.getInstance(props, null);

// -- Create a new message --
MimeMessage mimeMsg = new MimeMessage(session);

// -- Set the FROM and TO fields --
mimeMsg.setFrom(new InternetAddress(from));
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress
.parse(to, false));

// -- We could include CC recipients too --
// if (cc != null)
// msg.setRecipients(Message.RecipientType.CC
// ,InternetAddress.parse(cc, false));

// -- Set the subject and body text --
mimeMsg.setSubject(subject);
mimeMsg.setText(body);
if (attach != null && !attach.equalsIgnoreCase(""))
{
FileDataSource fds = new FileDataSource(attach);
mimeMsg.setDataHandler(new DataHandler(fds));
mimeMsg.setFileName(fds.getName());
}
// -- Set some other header information --
mimeMsg.setHeader("X-Mailer", "LOTONtechEmail");
mimeMsg.setSentDate(new Date());

// -- Send the message --
Transport.send(mimeMsg);
System.out.println("Message sent OK.");
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
还有两个jar的包,等我看看怎么发送附件吧!


----------------解决方案--------------------------------------------------------
[开源]javamail的jar包

那两个附件名字是mail.jar和activation.jar,怎么传到服务器上,我还要研究一下,请大家等等!


哈哈,我知道怎么弄了!

[此贴子已经被作者于2006-2-13 13:21:57编辑过]


----------------解决方案--------------------------------------------------------
学Struts最好是哪本书?大家给他意见。
----------------解决方案--------------------------------------------------------

为什么我写的发送邮件程序,浏览器说无法连接呢?


----------------解决方案--------------------------------------------------------

你不见原程序怎么说啊~~~~


----------------解决方案--------------------------------------------------------

顶一下


----------------解决方案--------------------------------------------------------
支持,等我看得懂的时候再来看。
----------------解决方案--------------------------------------------------------
不好意思
请问楼主JavaMail是什么呀?

----------------解决方案--------------------------------------------------------
高手帮忙
我在学习java,我对java面向对象编程的思想已经掌握了,但是我总觉得对java了解的太少太少,我想了解java比较底层的东西和其涉及的东西,但我又不知道他到底包括些什么东西?我想请高手帮忙提示我一下,
在学习语言这几年中体会到要想学好软件开发,必须把底层东西了解透,请高手帮忙指点一下,如何学好底层东西.那些是重点谢谢

----------------解决方案--------------------------------------------------------
  相关解决方案