james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rajender Vallapureddy <rajen...@hnexus.com>
Subject Re: bounce mail messages
Date Mon, 14 Jan 2013 08:34:37 GMT
Hi Eric,

I tried with Squirrel mail. First the problem is the value in the
mail_bytes itself is empty when the mail is getting stored in the
mysql james_mail table. I am able to see the postmaster message in the
inbox, but since the mail_bytes field is having empty value, I am
unable to open that email in any mail client.

I am pasting my custom mailet code.

package com.permitnonwebmail.mailet;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.UnknownHostException;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.james.core.MailImpl;
import org.apache.james.protocols.smtp.dsn.DSNStatus;
import org.apache.james.transport.mailets.AbstractNotify;
import org.apache.mailet.Mail;
import org.apache.mailet.MailAddress;
import org.apache.mailet.base.RFC2822Headers;
import org.apache.mailet.base.RFC822DateFormat;
import org.apache.mailet.base.mail.MimeMultipartReport;

public class PermitNonWebMailMailet extends AbstractNotify {
	private static final Log LOGGER = LogFactory.getFactory().getInstance(
			PermitNonWebMailMailet.class);
	private static final RFC822DateFormat rfc822DateFormat = new
RFC822DateFormat();
	private String messageString = null;

	/**
	 * Initialize the mailet
	 */
	public void init() throws MessagingException {
		super.init();
		messageString = getInitParameter("messageString",
				"Denied access for Web Mail Clients.\n");
	}

	/**
	 * Service does the hard work and bounces the mail in the format specified
	 * by RFC3464.
	 *
	 * @param mail
	 *            the mail to bounce
	 * @throws MessagingException
	 *             if a problem arises formulating the redirected mail
	 *
	 * @see org.apache.mailet.Mailet#service(org.apache.mailet.Mail)
	 */
	public void service(Mail mail) throws MessagingException {
		LOGGER.info("Permit Non Web Mail Mailet Initialized....");
		final MimeMessage msg = mail.getMessage();
		if (null != msg && !msg.equals("")) {
			System.out.println("bounce method called");
			// duplicates the Mail object, to be able to modify the new mail
			// keeping
			// the original untouched
			MailImpl newMail = new MailImpl(mail);
			try {
				// We don't need to use the original Remote Address and Host,
				// and doing so would likely cause a loop with spam detecting
				// matchers.
				try {
					newMail.setRemoteAddr(dns.getLocalHost().getHostName());
				} catch (UnknownHostException e) {
					newMail.setRemoteHost("localhost");
				}

				try {
					newMail.setRemoteHost(dns.getLocalHost().getHostAddress());
				} catch (UnknownHostException e) {
					newMail.setRemoteAddr("127.0.0.1");
				}

				if (mail.getSender() == null) {
					if (isDebug)
						log("Processing a bounce request for a message with an empty
reverse-path.  No bounce will be sent.");
					if (!getPassThrough(mail)) {
						mail.setState(Mail.GHOST);
					}
					return;
				}

				MailAddress reversePath = mail.getSender();
				if (isDebug)
					log("Processing a bounce request for a message with a reverse
path.  The bounce will be sent to "
							+ reversePath);

				Collection<MailAddress> newRecipients = new HashSet<MailAddress>();
				newRecipients.add(reversePath);
				newMail.setRecipients(newRecipients);

				if (isDebug) {
					log("New mail - sender: " + newMail.getSender()
							+ ", recipients: "
							+ arrayToString(newMail.getRecipients().toArray())
							+ ", name: " + newMail.getName() + ", remoteHost: "
							+ newMail.getRemoteHost() + ", remoteAddr: "
							+ newMail.getRemoteAddr() + ", state: "
							+ newMail.getState() + ", lastUpdated: "
							+ newMail.getLastUpdated() + ", errorMessage: "
							+ newMail.getErrorMessage());
				}

				// create the bounce message
				MimeMessage newMessage = new MimeMessage(
						Session.getDefaultInstance(System.getProperties(), null));

				MimeMultipartReport multipart = new MimeMultipartReport();
				multipart.setReportType("delivery-status");

				// part 1: descriptive text message
				MimeBodyPart part1 = new MimeBodyPart();
				part1.setText(messageString);
				multipart.addBodyPart(part1);

				// stuffing all together
				newMessage.setHeader(RFC2822Headers.CONTENT_TYPE,
						multipart.getContentType());
				newMessage.setContent(multipart);

				newMail.setMessage(newMessage);

				// Set additional headers
				setRecipients(newMail, getRecipients(mail), mail);
				setTo(newMail, getTo(mail), mail);
				setSubjectPrefix(newMail, getSubjectPrefix(mail), mail);

				if (newMail.getMessage().getHeader(RFC2822Headers.DATE) == null) {
					newMail.getMessage().setHeader(RFC2822Headers.DATE,
							rfc822DateFormat.format(new Date()));
				}

				setReplyTo(newMail, getReplyTo(mail), mail);
				setReversePath(newMail, getReversePath(mail), mail);
				setSender(newMail, getSender(mail), mail);
				setIsReply(newMail, isReply(mail), mail);

				// newMail.getMessage().saveChanges();
				getMailetContext().sendMail(newMail);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				newMail.dispose();
			}

			// ghosting the original mail
			if (getPassThrough(mail)) {
				mail.setState(Mail.GHOST);
			}
		}
	}

	/**
	 * Create a MimeBodyPart with a textual description for human readers.
	 *
	 * @param mail
	 * @return MimeBodyPart
	 * @throws MessagingException
	 */
	protected MimeBodyPart createTextMsg(Mail mail) throws MessagingException {
		MimeBodyPart part1 = new MimeBodyPart();
		StringWriter sout = new StringWriter();
		PrintWriter out = new PrintWriter(sout, true);
		StringBuffer bounceBuffer = new StringBuffer(128)
				.append("Denied access to Web Clients.");
		out.println(bounceBuffer.toString());
		out.println();
		part1.setText(sout.toString());
		return part1;
	}

	/**
	 * Guessing status code by the exception provided. This method should use
	 * the status attribute when the SMTP-handler some when provides it
	 *
	 * @param me
	 *            the MessagingException of which the statusCode should be
	 *            generated
	 * @return status the generated statusCode
	 */
	protected String getStatus(MessagingException me) {
		return DSNStatus
				.getStatus(DSNStatus.PERMANENT, DSNStatus.SECURITY_AUTH);
	}

	public String getMailetInfo() {
		return "DSNBounce Mailet";
	}

	/** Gets the expected init parameters. */
	protected String[] getAllowedInitParameters() {
		String[] allowedArray = { "debug", "passThrough", "messageString",
				"attachment", "sender", "prefix" };
		return allowedArray;
	}

	/**
	 * @return the <code>attachment</code> init parameter, or
	 *         <code>MESSAGE</code> if missing
	 */
	protected int getAttachmentType() throws MessagingException {
		return getTypeCode(getInitParameter("attachment", "message"));
	}

	/**
	 * @return <code>SpecialAddress.REVERSE_PATH</code>
	 */
	protected Collection<MailAddress> getRecipients() {
		Collection<MailAddress> newRecipients = new HashSet<MailAddress>();
		newRecipients.add(SpecialAddress.REVERSE_PATH);
		return newRecipients;
	}

	/**
	 * @return <code>SpecialAddress.REVERSE_PATH</code>
	 */
	protected InternetAddress[] getTo() {
		InternetAddress[] apparentlyTo = new InternetAddress[1];
		apparentlyTo[0] = SpecialAddress.REVERSE_PATH.toInternetAddress();
		return apparentlyTo;
	}

	/**
	 * @return <code>SpecialAddress.NULL</code> (the meaning of bounce)
	 */
	protected MailAddress getReversePath(Mail mail) {
		return SpecialAddress.NULL;
	}
}

Appreciate your help.

Thank you,
Regards,
Rajender

On Mon, Jan 14, 2013 at 1:36 PM, Eric Charles <eric@apache.org> wrote:
> Hi Rajender,
>
> I tried on my local trunk and get the original message attached to the
> bounce. It's true that my client (thunderbird) does not allow to open the
> attachement as a real mail, but shows it as plain text.
>
> Can you check the source of the bounce mail and see if this is not an issue
> with the mail client you are using.
>
> Thx, Eric
>
>
> On 13/01/2013 15:37, Rajender Vallapureddy wrote:
>>
>> Hi Eric,
>>
>> I replicated the DNSBounce to send custom bounce with postmaster..
>>
>> I am able to send / receive the postmaster message, but I am unable
>> see any body message in it.
>>
>> I am using mysql and in the "james_mail.mail_bytes" field all I am
>> seeing is empty blob and when I try to open that mail in squirrel mail
>> its giving me the imap error.
>>
>>
>> My class is exact the DNSBounce.java except for few conditions when to
>> send the bounce mail.
>>
>> MailImpl newMail = new MailImpl(mail);
>> newMail.setRemoteAddr(dns.getLocalHost().getHostName());
>> newMail.setRemoteHost(dns.getLocalHost().getHostAddress());
>> MailAddress reversePath = mail.getSender();
>> Collection<MailAddress> newRecipients = new HashSet<MailAddress>();
>> newRecipients.add(reversePath);
>> newMail.setRecipients(newRecipients);
>> MimeMessage newMessage = new
>> MimeMessage(Session.getDefaultInstance(System.getProperties(), null));
>> MimeMultipartReport multipart = new MimeMultipartReport();
>> multipart.setReportType("delivery-status");
>> MimeBodyPart part1 = new MimeBodyPart();
>> part1.setText(messageString); // message from this part is missing in
>> the mail body.
>> multipart.addBodyPart(part1);
>> newMessage.setHeader(RFC2822Headers.CONTENT_TYPE,
>> multipart.getContentType());
>> newMessage.setContent(multipart);
>> newMail.setMessage(newMessage);
>> setRecipients(newMail, getRecipients(mail), mail);
>> setTo(newMail, getTo(mail), mail);
>> setSubjectPrefix(newMail, getSubjectPrefix(mail), mail);
>> newMail.getMessage().setHeader(RFC2822Headers.DATE,
>> rfc822DateFormat.format(new Date()));
>> setReplyTo(newMail, getReplyTo(mail), mail);
>> setReversePath(newMail, getReversePath(mail), mail);
>> setSender(newMail, getSender(mail), mail);
>> setIsReply(newMail, isReply(mail), mail);
>> newMail.getMessage().saveChanges();
>> getMailetContext().sendMail(newMail);
>>
>>
>> Please suggest me what is causing the body message part to be missing
>> from the mail send.
>>
>> Appreciate your help.
>>
>> Thank you,
>> Regards,
>> Rajender
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
>> For additional commands, e-mail: server-dev-help@james.apache.org
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
> For additional commands, e-mail: server-dev-help@james.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message