Enviando e-mail com javax.mail

Estou tentando enviar um e-mail a partir de um servlet.

Conforme descrito abaixo:

[code]Properties p = new Properties();
p.put(“mail.host”, “smtp2.locaweb.com.br”);
Session session = Session.getInstance(p, null);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(“senhas@divecenter.com.br”));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(augusto@divecenter.com.br));
msg.setSubject(“Senha de acesso ao Site Dive Center”);

msg.setText(“xxxxxxxxxxx”);
msg.setText("");
msg.setText(“yyyyyyyyyyy”);

Transport.send(msg);[/code]

O código está funcionando, porém, o email recebido só tem a linha “yyyyyyy”. A minha intenção era que aparecesse a linha xxxxxxxxx, uma linha em branco e depois a linha yyyyyyyy.

Resumindo, não sei como saltar de linha, alguém pode me ajudar?

Grato,

Augusto

Tente da seguinte maneira:

String mensagem = "xxxxxxx \n \n yyyyyyyy";

message.setFrom(remetente);
message.setRecipient( Message.RecipientType.TO, destinatario );
message.setSubject (assunto);
message.setContent (mensagem, "text/plain");

Explicando o que o Ricardo Aguiar quis lhe dizer.

Ao criar um texto para o JavaMail, é aconselhável fazer algo como:

StringBuffer texto = new StringBuffer();
texto.append ("linha 1\r\n"); // note o \r\n para quebrar as linhas
texto.append ("linha 2\r\n");
texto.append ("linha 3\r\n");
msg.setText (texto.toString(), "ISO8859-1");

Aqui estou fazendo uma cópia do JavaDoc, ele diz duas coisas:

  • Se puder, use setText(String text, String charset) que é mais rápido
    Por exemplo:
    msg.setText (“seutexto”, “ISO8859-1”);
  • O texto é o texto completo da mensagem, não uma linha só (senão o nome seria algo como “writeLine” ou “printLine”).

void setText ( String text,
String charset
) throws MessagingException [inline]

Convenience method that sets the given String as this part’s content, with a MIME type of “text/plain” and the specified charset.

The given Unicode string will be charset-encoded using the specified charset. The charset is also used to set the “charset” parameter.


void setText ( String text ) throws MessagingException

Convenience method that sets the given String as this part’s content, with a MIME type of “text/plain”.

If the string contains non US-ASCII characters. it will be encoded using the platform’s default charset. The charset is also used to set the “charset” parameter.

Note that there may be a performance penalty if text is large, since this method may have to scan all the characters to determine what charset to use.

If the charset is already known, use the setText method that takes the charset parameter.

See also:
setText(String text, String charset)


Estou executando o mesmo codigo citado acima, porem, um erro me é retornado!

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: javax.activation.DataSource at Agricola.Email.jButton1_actionPerformed(Email.java:59) at Agricola.Email.mav$jButton1_actionPerformed(Email.java) at Agricola.Email$1.actionPerformed(Email.java:42) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234) at java.awt.Component.processMouseEvent(Component.java:5488) at javax.swing.JComponent.processMouseEvent(JComponent.java:3126) at java.awt.Component.processEvent(Component.java:5253) at java.awt.Container.processEvent(Container.java:1966) at java.awt.Component.dispatchEventImpl(Component.java:3955) at java.awt.Container.dispatchEventImpl(Container.java:2024) at java.awt.Component.dispatchEvent(Component.java:3803) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822) at java.awt.Container.dispatchEventImpl(Container.java:2010) at java.awt.Window.dispatchEventImpl(Window.java:1774) at java.awt.Component.dispatchEvent(Component.java:3803) at java.awt.EventQueue.dispatchEvent(EventQueue.java:463) at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:158) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149) at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

Oque pode Ser!

vc tah com problema no classpath de sua aplicação.
Verifique se vc tem algum desses jar da lista
http://www.jarfinder.com/index.php?class=javax.activation.DataSource&submit=search

se tiver, coloque-o no classpath

[]´s
Rodrigo

O cara, valeu obrigado!
A unica coisa agora é que o servico de e-mail postado nao esta respondendo!