java邮件保持传输对象连接 [英] java mail keeping Transport object connected
问题描述
我已经在我的代码中将这个代码写入了一个简单的Web应用程序中的类文件:
/ p>
@Resource(name =myMailServer)
private Session mailSession;
运输运输;
public boolean sendMail(String recipient,String subject,String text){
boolean exe = false;
属性p = new Properties();
字符串用户名=someone@gmail.com;
字符串密码=密码;
InitialContext c = null;
尝试
{
c = new InitialContext();
mailSession =(javax.mail.Session)c.lookup(java:comp / env / myMailServer);
}
catch(NamingException ne)
{
ne.printStackTrace();
}
try
{
Message msg = new MimeMessage(mailSession);
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(recipient,false));
msg.setSubject(subject);
msg.setText(text);
msg.setHeader(MIME-Version,1.0);
msg.setHeader(Content-Type,text / html);
msg.setHeader(X-Mailer,Recommend-It Mailer V2.03c02);
msg.saveChanges();
//Transport.send(msg);
if(transport == null){
transport = mailSession.getTransport(smtps);
System.out.println(+ transport.isConnected());
if(!transport.isConnected()){
transport.connect(username,password);
}
}
transport.sendMessage(msg,msg.getAllRecipients());
exe = true;
}
catch(AddressException e)
{
e.printStackTrace();
exe = false;
}
catch(MessagingException e)
{
e.printStackTrace();
exe = false;
finally {
/ * try {
if(transport!= null)
transport.close();
}
catch(MessagingException me){
me.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
} * /
}
return exe;
}
完整代码这里
现在我每次运行这段代码都需要一段时间才能连接邮件服务器/>
和行
System.out.println(+ transport.isConnected());
会打印一个假的
我如何保留对象传输,因为它会得到空值并进入块
if(transport == null){
或传输对象保持连接状态...
谢谢
Pradyut
代码应该是....
与传输对象的静态初始化
没有任何问题
但可以用函数
静态传输getTransport()方法很好
@Resource(name =myMailServer)
private Session mailSession;
静态运输运输;
public boolean sendMail(String recipient,String subject,String text){
boolean exe = false;
属性p = new Properties();
字符串用户名=someone@gmail.com;
字符串密码=密码;
InitialContext c = null;
尝试
{
c = new InitialContext();
mailSession =(javax.mail.Session)c.lookup(java:comp / env / myMailServer);
}
catch(NamingException ne)
{
ne.printStackTrace();
}
try
{
Message msg = new MimeMessage(mailSession);
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(recipient,false));
msg.setSubject(subject);
msg.setText(text);
msg.setHeader(MIME-Version,1.0);
msg.setHeader(Content-Type,text / html);
msg.setHeader(X-Mailer,Recommend-It Mailer V2.03c02);
msg.saveChanges();
//Transport.send(msg);
if(transport == null){
transport = mailSession.getTransport(smtps); (!!transport.isConnected()){
transport.connect(username,password);
}
transport.sendMessage(msg,msg.getAllRecipients());
exe = true;
}
catch(AddressException e)
{
e.printStackTrace();
exe = false;
}
catch(MessagingException e)
{
e.printStackTrace();
exe = false;
finally {
/ * try {
if(transport!= null)
transport.close();
}
catch(MessagingException me){
me.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
} * /
}
return exe;
}
谢谢
问候
Pradyut
How do i keep the java mail transport object alive or connected.
I have written this in my code in a simple class file inside a web application : -
@Resource(name = "myMailServer")
private Session mailSession;
Transport transport ;
public boolean sendMail(String recipient, String subject, String text) {
boolean exe = false;
Properties p = new Properties();
String username = "someone@gmail.com";
String password = "password";
InitialContext c = null;
try
{
c = new InitialContext();
mailSession = (javax.mail.Session) c.lookup("java:comp/env/myMailServer");
}
catch(NamingException ne)
{
ne.printStackTrace();
}
try
{
Message msg = new MimeMessage(mailSession);
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(recipient, false));
msg.setSubject(subject);
msg.setText(text);
msg.setHeader("MIME-Version" , "1.0" );
msg.setHeader("Content-Type" , "text/html" );
msg.setHeader("X-Mailer", "Recommend-It Mailer V2.03c02");
msg.saveChanges();
//Transport.send(msg);
if(transport == null) {
transport = mailSession.getTransport("smtps");
System.out.println("" + transport.isConnected());
if(!transport.isConnected()) {
transport.connect(username, password);
}
}
transport.sendMessage(msg, msg.getAllRecipients());
exe = true;
}
catch (AddressException e)
{
e.printStackTrace();
exe = false;
}
catch (MessagingException e)
{
e.printStackTrace();
exe = false;
}
finally {
/*try {
if(transport != null)
transport.close();
}
catch(MessagingException me) {
me.printStackTrace();
}
catch(Exception e) {
e.printStackTrace();
}*/
}
return exe;
}
the full code here
Now everytime i run this code it takes some time to connect with the mail server
and the line
System.out.println("" + transport.isConnected());
prints a false
How do i retain the object transport as it does gets null and into the block
if(transport == null) {
or the transport object remains connected...
Thanks
Pradyut
the code should be....
with a static initialization of transport object
without any problems
but can be good with a function
static Transport getTransport() method
@Resource(name = "myMailServer")
private Session mailSession;
static Transport transport ;
public boolean sendMail(String recipient, String subject, String text) {
boolean exe = false;
Properties p = new Properties();
String username = "someone@gmail.com";
String password = "password";
InitialContext c = null;
try
{
c = new InitialContext();
mailSession = (javax.mail.Session) c.lookup("java:comp/env/myMailServer");
}
catch(NamingException ne)
{
ne.printStackTrace();
}
try
{
Message msg = new MimeMessage(mailSession);
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(recipient, false));
msg.setSubject(subject);
msg.setText(text);
msg.setHeader("MIME-Version" , "1.0" );
msg.setHeader("Content-Type" , "text/html" );
msg.setHeader("X-Mailer", "Recommend-It Mailer V2.03c02");
msg.saveChanges();
//Transport.send(msg);
if(transport == null) {
transport = mailSession.getTransport("smtps");
}
if(!transport.isConnected()) {
transport.connect(username, password);
}
transport.sendMessage(msg, msg.getAllRecipients());
exe = true;
}
catch (AddressException e)
{
e.printStackTrace();
exe = false;
}
catch (MessagingException e)
{
e.printStackTrace();
exe = false;
}
finally {
/*try {
if(transport != null)
transport.close();
}
catch(MessagingException me) {
me.printStackTrace();
}
catch(Exception e) {
e.printStackTrace();
}*/
}
return exe;
}
Thanks
Regards
Pradyut
这篇关于java邮件保持传输对象连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!