Wrong encoding in message subject

Discussion in 'Java' started by Jan-Hendrik Lendholt, Dec 29, 2005.

  1. Hi everyone,

    we wrote a simple cronjob for our server, which connects to a database,
    gets some data eventually sends a mal via the java mail API.

    Until now everything worked fine, but we since we updated our OS the
    mail subject looks like:

    =?ANSI_X3.4-1968?Q?Noch_freie_Plaetze_fuer:_=3FHSV-Hallencup_?=
    =?ANSI_X3.4-1968?Q?2006=3F_am_8.1.2006_in_der?=

    Uhm, I am really confused. The *really* strange thing is:

    When I am executing the script via putty on the linux console, the
    subject is being shown correctly - but running as cronjob the subject
    is scrumbled ;-)

    The following java code is used to create the message:

    /*
    * Created on 17.03.2005
    *
    * TODO To change the template for this generated file go to
    * Window - Preferences - Java - Code Style - Code Templates
    */


    import java.util.*;
    import javax.mail.*;
    import javax.mail.internet.*;

    /**
    * @author Mathias Deutz
    *
    */
    public class SendMail
    {
    public static void send(String recipient, String subject, String
    message, String from)
    {
    try
    {
    Properties props = new Properties();
    props.put("IP-ADDRESS", "IP-ADDRESS");

    Session session = Session.getDefaultInstance( props );

    Message msg = new MimeMessage( session );

    InternetAddress addressFrom = new InternetAddress( from );
    msg.setFrom( addressFrom );

    InternetAddress addressTo = new InternetAddress( recipient );
    msg.setRecipient( Message.RecipientType.TO, addressTo );

    msg.setSubject( subject );
    msg.setContent( message, "text/html" );
    msg.saveChanges();

    Transport.send( msg );
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    }
    }


    And the following code sends the message:

    SendMail.send("",betreff,nachricht,"");

    I hope I could make myself clear.

    Thanks in advance!

    Jan Lendholt
     
    Jan-Hendrik Lendholt, Dec 29, 2005
    #1
    1. Advertising

  2. On 29 Dec 2005 02:01:33 -0800, Jan-Hendrik Lendholt wrote:
    > we wrote a simple cronjob for our server, which connects to a
    > database, gets some data eventually sends a mal via the java mail
    > API.
    >
    > Until now everything worked fine, but we since we updated our OS the
    > mail subject looks like:
    >
    >=?ANSI_X3.4-1968?Q?Noch_freie_Plaetze_fuer:_=3FHSV-Hallencup_?=
    >=?ANSI_X3.4-1968?Q?2006=3F_am_8.1.2006_in_der?=
    >
    > Uhm, I am really confused. The *really* strange thing is:
    >
    > When I am executing the script via putty on the linux console, the
    > subject is being shown correctly - but running as cronjob the subject
    > is scrumbled ;-)


    Cron jobs typically do not run in a login shell, and therefore they
    often have a different environment than you see from the login shell.
    It's not unlikely that you fail to set the proper locale settings in
    one of these, and that other one sees a system default.

    If you want to specify a default locale, you should be able to do so
    in your code as well. I have no experience with javax.mail, but have a
    look at the documentation for javax.mail.internet.MimeUtility for some
    clues.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Dec 29, 2005
    #2
    1. Advertising

  3. Jan-Hendrik Lendholt

    Igor Planinc Guest

    Jan-Hendrik Lendholt wrote:
    > Hi everyone,
    >
    > we wrote a simple cronjob for our server, which connects to a database,
    > gets some data eventually sends a mal via the java mail API.
    >
    > Until now everything worked fine, but we since we updated our OS the
    > mail subject looks like:
    >
    > =?ANSI_X3.4-1968?Q?Noch_freie_Plaetze_fuer:_=3FHSV-Hallencup_?=
    > =?ANSI_X3.4-1968?Q?2006=3F_am_8.1.2006_in_der?=


    MimeUtility.encodeText() is your friend. Just use it on the subject and you'll
    be OK. In fact, use it on every header value.
     
    Igor Planinc, Dec 29, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hardy Wang

    Encoding.Default and Encoding.UTF8

    Hardy Wang, Jun 8, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    19,061
    Jon Skeet [C# MVP]
    Jun 9, 2004
  2. Replies:
    1
    Views:
    23,576
    Real Gagnon
    Oct 8, 2004
  3. Dave Kelly

    A message with no subject.

    Dave Kelly, Aug 30, 2006, in forum: HTML
    Replies:
    2
    Views:
    356
    Brian Cryer
    Aug 31, 2006
  4. Paul Ponzelli
    Replies:
    4
    Views:
    482
    Paul Ponzelli
    Sep 28, 2007
  5. ioannisd

    Problem with the encoding of a CDO subject

    ioannisd, Dec 30, 2007, in forum: ASP General
    Replies:
    2
    Views:
    229
    ioannisd
    Dec 31, 2007
Loading...

Share This Page