Naver Cloud Outbound Mailer API ๊ฐ€์ด๋“œ For Java

    ๋ฐ˜์‘ํ˜•

    ๋„ค์ด๋ฒ„ ์•„์›ƒ๋ฐ”์šด๋“œ ๋ฉ”์ผ๋Ÿฌ๋ฅผ ์‚ฌ์šฉํ•  ์ผ์ด ์žˆ์–ด

    JAVA ๊ตฌํ˜„ ์˜ˆ์‹œ๋ฅผ ์ฐพ๋‹ค๊ฐ€ ๋‹ค์Œ์„ ๋ฐœ๊ฒฌํ•˜์˜€๋‹ค.

    yoshinari.tistory.com/39

     

    [Java] Naver Cloud Platform _ Cloud Outbound Mailer ์‚ฌ์šฉํ•˜๊ธฐ

    ์ด๋ฒˆ์—๋Š” ๋„ค์ด๋ฒ„ Cloud Outbound Mailer๋ฅผ ์ž๋ฐ”๋กœ ๊ฐœ๋ฐœ ํ•ด๋ณด๊ฒ ๋‹ค. ๊ตฌ๊ธ€๋ง์„ ํ•ด๋ณด๋ฉด ๋„ค์ด๋ฒ„์—์„œ ์ œ๊ณตํ•˜๋Š” gitHub์— ํŒŒ์ด์ฌ์„ ์‚ฌ์šฉํ•œ ๋ฒ„์ „ https://github.com/NaverCloudPlatform/outbound_mailer_python_sample So..

    yoshinari.tistory.com

    ์œ„ ๊ฒŒ์‹œ๊ธ€์„ ์ฐธ๊ณ ํ•˜์—ฌ ์ง„ํ–‰ํ–ˆ๋‹ค.

     

     

    ๋„ค์ด๋ฒ„ API ๊ฐ€์ด๋“œ์—์„œ SDK๋ฅผ ๋‹ค์šด๋ฐ›๋Š”๋‹ค.

    docs.gov-ncloud.com/ko/email/email-1-3.html

     

    ์„ค๋ช…์„œ

    ๋„ค์ด๋ฒ„ ํด๋ผ์šฐ๋“œ ํ”Œ๋žซํผ[๊ณต๊ณต๊ธฐ๊ด€์šฉ]์˜ ์ƒํ’ˆ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์„ ๋ณด๋‹ค ์ƒ์„ธํ•˜๊ฒŒ ์ œ๊ณตํ•˜๊ณ , ๋‹ค์–‘ํ•œ API์˜ ํ™œ์šฉ์„ ๋•๊ธฐ ์œ„ํ•ด [์„ค๋ช…์„œ]์™€ [API ์ฐธ์กฐ์„œ]๋ฅผ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. Cloud Outbound Mailer API ๊ฐœ

    docs.gov-ncloud.com

    ๋‹ค์šด ๋ฐ›์€ ์†Œ์Šค ๋‚ด

    nes-client-1.6.0G.jar ๋ฅผ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์ถ”๊ฐ€ํ•˜๊ณ  

    generated-code\src\main\java\com ์— ์žˆ๋Š” nbp ์†Œ์Šค ์ „์ฒด๋ฅผ ํ”„๋กœ์ ํŠธ์— ์ถ”๊ฐ€ํ•œ๋‹ค.

    ์ดํ›„ ์•„๋ž˜ ๋ชฉ๋ก์˜ jar ํŒŒ์ผ๋“ค์„ ์ „๋ถ€ ๋‹ค์šด๋ฐ›๊ฑฐ๋‚˜ pom ์— ์ถ”๊ฐ€ํ•˜์—ฌ ํ”„๋กœ์ ํŠธ๋กœ ๊ฐ€์ ธ์˜จ๋‹ค.

     

    <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
    		<dependency>
    			<groupId>commons-codec</groupId>
    			<artifactId>commons-codec</artifactId>
    			<version>1.11</version>
    		</dependency>
    		<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
    		<dependency>
    			<groupId>org.apache.commons</groupId>
    			<artifactId>commons-lang3</artifactId>
    			<version>3.7</version>
    		</dependency>
    		<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
    		<dependency>
    			<groupId>com.fasterxml.jackson.core</groupId>
    			<artifactId>jackson-annotations</artifactId>
    			<version>2.9.3</version>
    		</dependency>
    		<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    		<dependency>
    			<groupId>com.fasterxml.jackson.core</groupId>
    			<artifactId>jackson-databind</artifactId>
    			<version>2.9.3</version>
    		</dependency>
    		<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-xml -->
    		<dependency>
    			<groupId>com.fasterxml.jackson.dataformat</groupId>
    			<artifactId>jackson-dataformat-xml</artifactId>
    			<version>2.9.3</version>
    		</dependency>
    		<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
    		<dependency>
    			<groupId>com.googlecode.json-simple</groupId>
    			<artifactId>json-simple</artifactId>
    			<version>1.1.1</version>
    		</dependency>
    		<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/logging-interceptor -->
    		<dependency>
    			<groupId>com.squareup.okhttp3</groupId>
    			<artifactId>logging-interceptor</artifactId>
    			<version>3.9.1</version>
    		</dependency>
    		<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
    		<dependency>
    			<groupId>com.squareup.okhttp3</groupId>
    			<artifactId>okhttp</artifactId>
    			<version>3.9.1</version>
    		</dependency>
    		<!-- https://mvnrepository.com/artifact/com.squareup.okio/okio -->
    		<dependency>
    			<groupId>com.squareup.okio</groupId>
    			<artifactId>okio</artifactId>
    			<version>2.2.2</version>
    		</dependency>
    		<!-- https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-runtime -->
    		<dependency>
    			<groupId>org.jetbrains.kotlin</groupId>
    			<artifactId>kotlin-runtime</artifactId>
    			<version>1.1.1</version>
    		</dependency>

     

    ์ดํ›„, ์†Œ์Šค ๋‚ด์— ์žˆ๋Š” credentials.properties ์— ๋ฐœ๊ธ‰๋ฐ›์€ API ํ‚ค์™€ Secret ํ‚ค๋ฅผ ์ž‘์„ฑ ํ›„, ํ”„๋กœ์ ํŠธ์— ์ถ”๊ฐ€ํ•œ๋‹ค.

     

    ๊ทธ ํ›„, ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ๋„ฃ๊ณ  ์‹คํ–‰ํ•˜๋ฉด ๋œ๋‹ค.

    ํ”„๋กœํผํ‹ฐ ๊ฒฝ๋กœ๋Š” ์‹ค์ œ ํ”„๋กœ์ ํŠธ์— ์กด์žฌํ•˜๋Š” ๊ฒฝ๋กœ๋ฅผ ์‚ฝ์ž…ํ•˜๋ฉด๋œ๋‹ค.

     

    ์•„๋ž˜๋Š” ์‹ค์ œ ์˜ˆ์ œ ๋™์ž‘ ์‹œ, ๋™์ž‘ํ–ˆ๋˜ ์ฝ”๋“œ์ด๋‹ค.

     

    public static void createMailRequest() {
            String propertiesLocation = "{properties ํŒŒ์ผ ์œ„์น˜}"
    		ApiClient apiClient = new ApiClient.ApiClientBuilder()
            	.addMarshaller(JsonMarshaller.getInstance())
                .addMarshaller(XmlMarshaller.getInstance())
                .addMarshaller(FormMarshaller.getInstance())
                .setCredentials(new PropertiesFileCredentialsProvider(propertiesLocation)
                .getCredentials()).setLogging(true).build();
    		// API ๊ฐ์ฒด ์ƒ์„ฑ
    		V1Api apiInstance = new V1Api(apiClient);
    		// ๋ฉ”์ผ ์ˆ˜์‹ ์ž ๋ฆฌ์ŠคํŠธ ์ž‘์„ฑ.
    		List<EmailSendRequestRecipients> esrrList = new ArrayList<EmailSendRequestRecipients>();
    		// ์ˆ˜์‹ ์ž ๋ฆฌ์ŠคํŠธ ์„ค์ •
    		EmailSendRequestRecipients esrr = new EmailSendRequestRecipients();
    		esrr.setAddress("์ˆ˜์‹ ์ž์ด๋ฉ”์ผ");
    		esrr.setName("์ˆ˜์‹ ์ž์ด๋ฆ„");
    		esrr.setType("R"); // ํƒ€์ž… ๊ณ ์ • : R ( ์ผ๋ฐ˜ ์ˆ˜์‹ ์ž )
    		esrrList.add(esrr);
    		EmailSendRequest requestBody = new EmailSendRequest();
    		// ๋ฉ”์ผ ๋ณธ๋ฌธ ์ž‘์„ฑ
            String body = "{๋ฉ”์ผ ๋‚ด์šฉ, html ์ฝ”๋“œ ๋ฐ ๋‚ด์šฉ ๋“ฑ}"
    		// ์š”์ฒญ์— ๋ณธ๋ฌธ ๋ฐ ์ˆ˜์‹ ์ž ๋ฆฌ์ŠคํŠธ ์„ค์ •
    		requestBody.setBody(body);
    		requestBody.setRecipients(esrrList);
    		// requestBody.setTemplateSid({templateID}); ํ…œํ”Œ๋ฆฟ ์•„์ด๋”” ์ถ”๊ฐ€.
    		// ๋ฐœ์‹ ์ž ์ •๋ณด ์„ค์ •
    		requestBody.setSenderAddress("๋ฐœ์‹ ์ž์ด๋ฉ”์ผ");
    		requestBody.setSenderName("๋ฐœ์‹ ์ž๋ช…");
    		// ๋ฉ”์ผ ์ œ๋ชฉ ์„ค์ •
    		requestBody.setTitle(title);
    		// ์ „์†ก ํ™•์ธ ๊ธฐ๋Šฅ ์„ค์ •
    		requestBody.setConfirmAndSend(false);
    		// ์ „๋‹ฌ ์–ธ์–ด ์„ค์ •
    		String X_NCP_LANG = "ko-KR"; // String | ์–ธ์–ด (ko-KR, en-US, zh-CN), default:en-US
    		try {
    			// Handler Successful response
    			ApiResponse<EmailSendResponse> res = apiInstance.mailsPost(requestBody, X_NCP_LANG);
    		} catch (ApiException e) {
    			// Handler Failed response
    			int statusCode = e.getHttpStatusCode();
    			Map<String, List<String>> responseHeaders = e.getHttpHeaders();
    			InputStream byteStream = e.getByteStream();
    			e.printStackTrace();
    		} catch (SdkException e) {
    			// Handle exceptions that occurred before communication with the server
    			e.printStackTrace();
    		}
     }
    728x90
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€