Skip to Content
avatar image
Former Member

sending mail exception

i have the following code...

i try to send to an non-exisiting email address....but i cannot catch inside the exception...pls advice

non-exisiting email address

asdfdfaf@yahoo.com

xcxzczczcxz@yahoo.com

private boolean sendAuthenticate(OutMailBean bean) throws RemoteException {
		boolean sendflag=false;
		
			ArrayList ackList = new ArrayList();
		    ArrayList detailsList = new ArrayList();
		    ArrayList outMailList = new ArrayList();
		    
			
			net.emisolutions.adaptermail.model.OutMailBean outMailBean = new net.emisolutions.adaptermail.model.OutMailBean();
			
			Address[] address = null;
			Util util = new Util();
			Multipart mp = null;
			Transport tr = null;
			Session sess = null;
			String[] messages = bean.getMessages();
		
			System.err.println(new Date() + " sendAuthenticate(OutMailBean): From=" + bean.getFrom());

			try {
				InitialContext ctx = new InitialContext();
				Properties props = System.getProperties();
				props.setProperty("mail.smtp.auth", "true");
				sess =(Session) javax.rmi.PortableRemoteObject.narrow(ctx.lookup("java:comp/env/mail/MailSession"), Session.class);
				sess = Session.getDefaultInstance(props, null);
				Message msg = new MimeMessage(sess);
				msg.setFrom(new InternetAddress(bean.getFrom()));
				
				outMailBean.setEmailId(util.getUniqueId());
				
				msg.setRecipients(Message.RecipientType.TO, extractRecipientAddresses(bean.getTo(), ackList, util, outMailBean.getEmailId()));
				msg.setSubject(bean.getSubject());
				
				boolean hasAttachment = (bean.getAttachedFileName() != null) && (bean.getAttachedFileName().length() > 0);
				
				boolean isMultiPart = (messages != null) && (messages.length > 1);

				// adding an attachment makes the message multipart 
				if (isMultiPart || hasAttachment) {
					OutMailDetailsBean outMailDetailBean = new OutMailDetailsBean();
					mp = new MimeMultipart();
					// add text parts
					if (messages != null) {
						for (int i = 0; i < messages.length; i++) {
							MimeBodyPart bodyPart = new MimeBodyPart();
							bodyPart.setContent(messages<i>, "text/plain");
							mp.addBodyPart(bodyPart);
						}
					}
					// attach the file to the message if needed
					if (hasAttachment) {
						// avoid the case with no text parts
						MimeBodyPart bodyPart = new MimeBodyPart();
						bodyPart.setContent("Attachment enclosed.", "text/plain");
						mp.addBodyPart(bodyPart);
						// the part with the file
						FileDataSource fds = new FileDataSource(bean.getAttachedFileName());
						MimeBodyPart attachmentBodyPart = new MimeBodyPart();
						DataHandler dh = new DataHandler(fds);
						attachmentBodyPart.setDataHandler(dh);
						attachmentBodyPart.setFileName(fds.getName());
						mp.addBodyPart(attachmentBodyPart);
						//outMailDetailBean.setAttachmentName(bean.getAttachedFileName());
						outMailDetailBean.setAttachment(util.convertDataHandlerToButeArr(dh));		
					}
					msg.setContent(mp);
					outMailDetailBean.setAttachmentName(bean.getAttachedFileName());
					outMailDetailBean.setAttachmentId(util.getUniqueId());
					outMailDetailBean.setEmailId(outMailBean.getEmailId());
					detailsList.add(outMailDetailBean);
				}else {
					if ((messages != null) && (messages.length > 0)) {
						msg.setContent(messages[0], "text/plain");
					} else {
						msg.setContent("", "text/plain");
					}
				}

				msg.setSentDate(new GregorianCalendar().getTime());
				msg.saveChanges();
					tr = sess.getTransport("smtp");
					//username & password needed for authentication
					tr.connect(bean.getHost(), bean.getUsername(), bean.getPassword());
					System.err.println("Host:" +bean.getHost() +" " +"Username:" +bean.getUsername() +"PWD:" +bean.getPassword());
					address = msg.getAllRecipients();
					tr.sendMessage(msg, address);
					tr.close();
					System.err.println("after sent");
				
				
				
				outMailBean.setDateIn(util.getDateIn());
				outMailBean.setDateSend(util.convertDateToStringFormat(msg.getSentDate()));
				System.err.println("after setDateSemd");
				outMailBean.setMessage(util.convertStringBufferToByteArr(bean.getMessages()));
				System.err.println("after setMessage");
				outMailBean.setSender(bean.getFrom());
				outMailBean.setSubject(bean.getSubject());
				outMailBean.setTimeIn(util.convertStringToLongDateFormat(outMailBean.getDateIn()));
				outMailBean.setTimeSend(util.convertStringToLongDateFormat(outMailBean.getDateSend()));
				//added by won...empty string is not allowed ""
				outMailBean.setImportance("1");
				outMailBean.setDateCreated(outMailBean.getDateIn());
				outMailBean.setTimeCreated(util.convertStringToLongDateFormat(outMailBean.getDateIn()));
				outMailList.add(outMailBean);
				System.err.println("after addOutMailBan");
				
				System.err.println("outMailList size= " + outMailList.size());
				System.err.println("detailList size= " +  detailsList.size());
				System.err.println("ackMailList size= " + ackList.size());
				
				sendflag=true;
			}catch(AddressException e){
				System.err.println("Address Exception:" +e.getMessage());
			}catch(SMTPSendFailedException e){  //if the send failed because of an SMTP command error /invalid addresses
				System.err.println("SMTPSendFailedException: " +e.getMessage());
				Address[] invalidAddr  = e.getInvalidAddresses();
				if (invalidAddr != null) {
					 for (int i = 0; i < invalidAddr.length; i++){
						System.err.println(new Date() + " SMTPSendFailedException: " +invalidAddr<i>);
						for(Iterator it = ackList.iterator(); it.hasNext(); ){
							OutMailAckBean ackBean = (OutMailAckBean)it.next();
							if(ackBean.getReceipient().equals(invalidAddr<i>)){
								ackBean.setAckDelivery(util.getMNOK());	
							}
						}
					 }
			    }

			}catch(SendFailedException e){  // if the send failed because of mail server access prob, etc
				System.err.println("SendFailedException: " +e.getMessage());
				Address[] validUnsentAddr = e.getValidUnsentAddresses();
				if (validUnsentAddr != null) {
					 for (int i = 0; i < validUnsentAddr.length; i++){
						System.err.println(new Date() + " SendFailedException "+ validUnsentAddr<i>);
						for(Iterator it = ackList.iterator(); it.hasNext(); ){
							OutMailAckBean ackBean = (OutMailAckBean)it.next();
							if(ackBean.getReceipient().equals(validUnsentAddr<i>)){
								ackBean.setAckDelivery(util.getMNOK());	
							}
						}	
					 }
				}
				
			}catch(MessagingException e){
				System.err.println("Messaging Exception:" +e.getMessage());
			}catch(Exception e){
				System.err.println(new Date() + " Send Mail Exception : " + e);	
			
			}finally{
				//TODO insert into table
				System.err.println("before insert outMailList");
				//*********commented out by won*********************************************
				//mailSLS.insertIntoTable(outMailList, detailsList, ackList);
				System.err.println("after insert outMailList");
				System.err.println("process mail");
				//***********commented out by won  --->process mail/process function is call by R3***********************************************************************
				//mailSLS.processmail();    
			}
	
			System.err.println("check sendflag : " +sendflag);
			return sendflag;
		}

Message was edited by:

yzme yzme

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • avatar image
    Former Member
    Mar 30, 2007 at 09:07 AM

    Yzme,

    Check the following link for reference

    Email Problem

    also check the following code

    package com.msit.webad.mail;

    import java.util.*;

    import javax.mail.*;

    import javax.mail.internet.*;

    import java.text.DateFormat;

    import java.text.SimpleDateFormat;

    class AutoMail extends TimerTask {

    public void run() {

    java.util.Date dt = new Date();

    java.util.Calendar c = java.util.Calendar.getInstance();

    c.setTime(dt);

    c.add(Calendar.DATE,-7);

    DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);

    String s = DateFormat.getDateInstance(DateFormat.MEDIUM).format(dt);

    dt.before(dt);

    String body = "This is an Autogenerated Mail."+

    "some message"+

    "for the period from "df.format(c.getTime())"to" +s;

    ResourceBundle web = ResourceBundle.getBundle("webads");

    String to = web.getString("yzme@mail.com");

    sendMail("anil@mail.com", to, "message", body);

    }// end of run...

    public boolean sendMail(String fromID, String toID, String mailSubject,

    String mailBody) {

    // Write mail sending logic here

    String from = fromID;

    String to = toID;

    String subject = mailSubject;

    String body = mailBody;

    String host = "host name";

    // create some properties and get the default Session

    try {

    Properties props = new Properties();

    props.put("mail.smtp.host", host);

    Session session = Session.getDefaultInstance(props, null);

    // create a message

    Message msg = new MimeMessage(session);

    msg.setFrom(new InternetAddress(from));

    InternetAddress[] address = { new InternetAddress(to) };

    msg.setRecipients(Message.RecipientType.TO, address);

    msg.setSubject(subject);

    msg.setSentDate(new Date());

    msg.setText(body);

    Transport.send(msg);

    // System.out.println("Mail has been sent");

    // if mail sent , return true, else return false

    return true;

    } catch (Exception mex) {

    System.out.println("Error " + mex);

    return false;

    }

    }// end of sendMail...

    }

    regards

    Anil Dichpally

    ps:change the code according to the requirement and then try it.

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      Hi yzme,

      excuse me for delayed reply.

      Have you included the required jar files. i.e.mail.jar and activation.jar files.

      if not please include them and let me know the status.

      regards

      Anil Dichpally

  • Mar 30, 2007 at 02:55 PM

    Hi yzme,

    The question is why no exception is thrown if you send an email to xcxzczczcxz@yahoo.com? An AddressException is only thrown when the syntax of the email address doesn't comply with RFC822.

    The point is there's nothing wrong with this email address. Your SMTP server will just send your email to the yahoo mail servers. The yahoo server will find out it can't deliver the email. Then the yahoo server has two options: bounce the message or ignore it. If it bounces the message, it appears in the email folder of the sender of the email message. For security reasons it's often better to just ignore it.

    Also, there's no reliable way to find out if an email address exists.

    Kind regards,

    Sigiswald

    Add comment
    10|10000 characters needed characters exceeded