简体   繁体   中英

Sending email in Java by using database

I am currently doing a project where I am required to send an email to the specific address taken from database. However, the column "email" in database does not actually contain emails, but names instead. So in database there are full names in russian language like

"Иванов Александр" which is "Ivanov Alexandr". So when I type this name in the outlook it automatically finds his email: AIvanov@domainname.com. But in my java code when I use name "Иванов Александр" i keep getting error.

Here is my java code

File[] listOfFiles = outDir.listFiles();
    if (outDir.isDirectory()) {
        if (outDir.list().length > 0) {
            for (File file : listOfFiles) {
                    Session session_m = Session.getDefaultInstance(props, null);
                    MimeMessage message = new MimeMessage(session_m);
                    message.setFrom (new InternetAddress("mmm@domainname.com", "mmm@domainname.com"));
                    InternetAddress i = new InternetAddress("\""+email+"\"", false);
                    message.addRecipient(Message.RecipientType.TO, i);
                    message.setSubject("test");
                    message.setText("test");
                    message.setHeader("Content-Type","text/plain;charset=windows-1251");

                    MimeBodyPart mbp1 = new MimeBodyPart();
                    FileDataSource fds = new FileDataSource(file);
                    mbp1.setDataHandler(new DataHandler(fds));
                    mbp1.setFileName(fds.getName());

                    Multipart mp = new MimeMultipart();
                    mp.addBodyPart(mbp1);

                    System.out.println("[EmailHandler] Attaching the following file to email: " + fds.getName());
                    message.setContent(mp);

                    SMTPTransport t = (SMTPTransport)session_m.getTransport("smtp");
                    t.connect("mail.domainname.com", "main@domainname.com", null);
                    System.out.println("[EmailHandler] Sending email... ");
                    t.sendMessage(message, message.getAllRecipients());
                    file.delete();
                    Thread.sleep(3000);
                }
            } else {
                System.out.println("[EmailHandler] Folder " + outDir.getName() + " is empty... nothing to attach");
            }
        } else {
            System.out.println("Folder not found... Check the path");
        }

In this code the String email is Иванов Александр.

And I kept getting this error

javax.mail.internet.AddressException: Local address contains control or whitespace in string ``Иванов Александр''

So would like to know the ways I can make this string go through.

Thank you.

The outlook uses its address book to map a name to one of the email. That is why it is working fine, if you manually try creating a new email and just put the name. Outlook simply do a lookup in the address book and find out the email address.

However, this is not the same with a java program. The program needs exact email address to send an email. Now there could be many ways to find out email address.

The simplest approach is to store the email address in one of the database table. If the person is associated with the company's SMTP system/active directory; you could use java smtp API / active directory APIs to find out the email or alias (usually be the part of email id before @) and then create email id to be used into the program to send email.

You need to provide a valid mail address. If you are using a fixed structure for your mails just convert the name to latin characters and append @domain.com If you are not using any rules for your mail adress creation then I suggest you to add an email field to the database

Don't know if this resolves your question though

Regards

Your program have to

  1. Validate e-mail. It can be implemented using regular expression: Using a regular expression to validate an email address

  2. Get email from contact list when field contains only name. There are two library for working with outlook contacts: Open source java library to read outlook emails, calendar etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM