Check the domain by checking the MX record of the email address with java

When you accept emails on the WEB I think that a lot of spam mails will fly mixed with the correct mails.

Even if you try to reply to the "Reception completed!" Email after accepting the email If you are a spammer There is no such address! Every day I see a pile of error returns piled up ... Can't this be done? !!

Since the part before the @ mark in the email address corresponds to the user I don't know until I send it

at least Let's check if the part behind the @ mark is a domain that really exists!

There used to be that, and it feels like it was built in java.

There is no particular explanation.

import java.util.Hashtable;

import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

public class MXLookup {
	public static void main(String[] args) {
		exec("yahoo.jp");
		exec("yahoo.co.jp");
		exec("126.com");
		exec("127.com");
	}
	private static void exec(String hostname) {
		System.out.println( hostname + " : " + doLookup( hostname ) + "" );
	}
	private static String doLookup( String hostName ) {
		try {
			Hashtable<String,String> env = new Hashtable<String,String>();
			env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
			DirContext ictx = new InitialDirContext( env );
			Attributes attrs = ictx.getAttributes( hostName, new String[] { "MX" });
			Attribute attr = attrs.get( "MX" );
			if( attr == null ){
				return null;
			}
			StringBuilder sb = new StringBuilder();
			for(int i=0;i<attr.size();i++){
				if(sb.length() > 0){
					sb.append(", ");
				}
				sb.append(attr.get(i));
			}
			return sb.toString();
		} catch (NamingException e) {
			return null;
		}
	}

}

By the way, the created system Eat incoming mail to shell This Java is called from the shell that is kicked when the mail is received in It's like filtering email senders.

that's all For your reference.

Recommended Posts

Check the domain by checking the MX record of the email address with java
Check the behavior of Java Intrinsic Locks with bpftrace
Check the contents of the Java certificate store
Check the contents of params with pry
Memo: [Java] Check the contents of the directory
Verify the uniqueness of your email address
[Java] Check the number of occurrences of characters
Write ABNF in Java and pass the email address
CI the architecture of Java / Kotlin applications with ArchUnit
[Java] Check the JDK version of the built war file
Monitor the internal state of Java programs with Kubernetes
Check the result of generic parameter inference with JShell
The story of making dto, dao-like with java, sqlite
Replace only part of the URL host with java
Find the address class and address type from the IP address with Java
Command to check the number and status of Java threads
Check the operation of two roles with a chat application
Check the status of Java application without using monitoring tool
Traps brought about by the default implementation of the Java 8 interface
[Java] Check the difference between orElse and orElseGet with IntStream
Summary of values returned by the Spliterator characteristics method #java
Be sure to compare the result of Java compareTo with 0
Graph the sensor information of Raspberry Pi in Java and check it with a web browser
Switch the version of java installed by SDKMAN when moving directories
The story of not knowing the behavior of String by passing Java by reference
Try Hello World with the minimum configuration of Heroku Java spring-boot
I tried to check the operation of gRPC server with grpcurl
The point of addiction when performing basic authentication with Java URLConnection
[Java] Appropriate introduction by the people of Tempa Java Part 0 (Code rules)
Process the motion detected by the motion sensor HC-SR501 with Raspberry Pi 3 & Java
Overwrite upload of file with the same name with BOX SDK (java)
How to check for the contents of a java fixed-length string
Is the version of Elasticsearch you are using compatible with Java 11?
By checking the operation of Java on linux, I was able to understand compilation and hierarchical understanding.