开发者

how do I send mail in gwt?

I have the following code runnning on the server over gwt rpc:

Properties props = new Properties();

    props.setProperty("mail.transport.protocol",开发者_开发百科 "smpt");
    props.setProperty("mail.smtp.port", "25");
    props.setProperty("mail.host", "smtp.random.com");
    props.setProperty("mail.user", "user@random.com");
    props.setProperty("mail.password", "passwd");

    Session mailSession = Session.getDefaultInstance(props, null);
    Transport transport = mailSession.getTransport();

    MimeMessage message = new MimeMessage(mailSession);
    message.setSubject(subject);
    message.setContent(mailMessage, "text/plain");
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(adress));

    transport.connect();
    transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
    transport.close();

but Im getting the error:

com.google.gwt.user.client.rpc.SerializationException: Type 'javax.mail.NoSuchProviderException' was not included in the set of types which can be serialized by this SerializationPolicy or its Class object could not be loaded. For security purposes, this type will not be serialized.: instance = javax.mail.NoSuchProviderException: Unable to locate provider for protocol: smtp

how do I fix it?


GWT Java code is compiled to Javascript and runs in your browser. Not all Java classes are available on the client-side. There's a nice write-up about this here. You should instead consider making a GWT RPC call to your server where you can run your code to generate e-mails.

Here's an example of how to use GWT RPC: http://code.google.com/webtoolkit/doc/latest/tutorial/RPC.html


Without seeing the method signature my guess is that your method signature throws the NoSuchProviderException

fix:

public void sendMail(...) throws Exception{
  try{
    // mail code here...


  } catch(NoSuchProviderException e){
    throw new Exception(e);
  }
}

or create your exception classes and use those:

public class MailException extends Exception{
   public MailException(){}
   public MailException(String msg){
     super(msg);
   }
}


You seem to be returning the NoSuchProviderException back to the client-side and that does not work. It is happening somewhere outside this code snippet.

Original problem (throwing the NoSuchProviderException) is that typo on the first property line. The protocol is called 'smtp' not 'smpt'.

If you are still experiencing problems. Check that you have the right versions of mail jar and activation jar available in your server classpath.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜