SMTP를 이용해 메일 기능을 구현시 첨부 파일 및 본문에 이미지가 포함되어 전송될경우

Google Mail 같은경우에는 이미지가 짤리는 현상이 발생한다.

이와 같은 현상이 발생할 경우에는 아래 소스와 같이 처리하면 된다.

 

- Main 메일 전송코드

//Mail Send Main Code
MailMessage mail = new MailMessage();

mail.From = new Mailaddress("FormEmail@mail.com");

AlternateView alterView = ContentToAlternateView(html_body);
mail.AlternateViews.add(alterView);

- Base64 인코딩 이미지 첨부 

private static AlternateView ContentToAlternateView(string content)
{
            var imgCount = 0;
            List<LinkedResource> resourceCollection = new List<LinkedResource>();
            foreach(Match m in Regex.Matches(content, "<img(?<value>.*?)>"))
            {
                imgCount++;
                var imgContent = m.Groups["value"].Value;
                string type = Regex.Match(imgContent, ":(?<type>.*?);base64,").Groups["type"].Value;
                string base64 = Regex.Match(imgContent, "base64,(?<base64>.*?)\"").Groups["base64"].Value;

                if(String.IsNullOrEmpty(type) || String.IsNullOrEmpty(base64))
                {
                    continue;
                }

                var replacement = " src=\"cid:" + imgCount + "\"";
                content = content.Replace(imgContent, replacement);
                var tempResource = new LinkedResource(Base64ToImageStream(base64), new ContentType(type))
                {
                    ContentId = imgCount.ToString()
                };

                resourceCollection.Add(tempResource);

            }

            AlternateView alternateView = AlternateView.CreateAlternateViewFromString(content, null, MediaTypeNames.Text.Html);

            foreach(var item in resourceCollection)
            {
                alternateView.LinkedResources.Add(item);
            }

            return alternateView;

}


 public static Stream Base64ToImageStream(string base64String)
{
            byte[] imageBtyes = Convert.FromBase64String(base64String);
            MemoryStream ms = new MemoryStream(imageBtyes, 0, imageBtyes.Length);
            return ms;
}

 

원본글 : https://stackoverflow.com/questions/39407474/add-attachment-base64-image-in-mailmessage-and-read-it-in-html-body

 

SMALL

'언어 > C#' 카테고리의 다른 글

[Error Report] Asp.Net Core IIS 게시오류  (0) 2021.10.01
[숙제] 2021.09.29  (0) 2021.09.29
C# Build Error  (0) 2020.09.07
C# 메일전송 참조  (0) 2019.11.20
C# ] Dll 참조  (0) 2019.10.02

Asp.net Core 로 개발된 웹 사이트를 게시하여 IIS에 배포 하였는데도 오류가 발생

 

IIS 에 Asp.net Core 설정 오류.

 

Step1. 응용프로그램 풀 확인

  - .Net CLR 버전이 관리코드없음으로 되어있는지 확인.

Step2. 구성편집기 확인

  - 구성편집기에 System.webServer/aspNetCore 가 설치되어있는지 확인.

SMALL

'언어 > C#' 카테고리의 다른 글

C#] SMTP 파일 첨부 및 본문 이미지(Base64 인코딩) 전송  (0) 2021.12.07
[숙제] 2021.09.29  (0) 2021.09.29
C# Build Error  (0) 2020.09.07
C# 메일전송 참조  (0) 2019.11.20
C# ] Dll 참조  (0) 2019.10.02

C# Asp.net Core

View에서 활용한 inject 에 대해 설명하세요.

 

 

참고

 

SMALL

'언어 > C#' 카테고리의 다른 글

C#] SMTP 파일 첨부 및 본문 이미지(Base64 인코딩) 전송  (0) 2021.12.07
[Error Report] Asp.Net Core IIS 게시오류  (0) 2021.10.01
C# Build Error  (0) 2020.09.07
C# 메일전송 참조  (0) 2019.11.20
C# ] Dll 참조  (0) 2019.10.02

Error :

Couldn't process file ******.resx due to its being in the Internet or Restricted zone or having themark of the web on the file.

해결 방법

Project 폴더에 *.resx 파일을 검색하여, 해당 파일 속성에 들어가 차단 해제를 클릭하고 저장 후

Projcet 정리 후 ReBuild 합니다.

SMALL

'언어 > C#' 카테고리의 다른 글

[Error Report] Asp.Net Core IIS 게시오류  (0) 2021.10.01
[숙제] 2021.09.29  (0) 2021.09.29
C# 메일전송 참조  (0) 2019.11.20
C# ] Dll 참조  (0) 2019.10.02
Show / ShowDialog  (0) 2019.10.01

public static void FileMailSend(string filePath, string msg)
        {
            MailMessage mail = new MailMessage();


            //보내는 사람 이메일
            mail.From = new MailAddress("impelhaihm@gmail.com");
            
            //반는사람 이메일
            mail.To.Add("ToMailAdress");
            //메일제목
            mail.Subject = "SpcAutoDrafts Error";
            //메일내용
            mail.Body = msg;

            //첨부파일
            System.Net.Mail.Attachment attachment;

            //첨부파일 붙이기
            attachment = new System.Net.Mail.Attachment(filePath);
            mail.Attachments.Add(attachment);

            //gmail 포트 설정
            SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
            
            //SSL 설정 (gmail 은 ture로 설정, 메일 서버 세팅에 따라 다름)
            smtp.EnableSsl = true;

            //gamil 인증을 위한 id/pw (보내는사람 정보 넣기)
            smtp.Credentials = new NetworkCredential(" Email Adress ", " passworld ");
            try
            {
                smtp.Send(mail);
                

            }catch
            {
                MessageBox.Show("메일발송실패");
            }

        }

SMALL

'언어 > C#' 카테고리의 다른 글

[숙제] 2021.09.29  (0) 2021.09.29
C# Build Error  (0) 2020.09.07
C# ] Dll 참조  (0) 2019.10.02
Show / ShowDialog  (0) 2019.10.01
연산자  (0) 2019.10.01

Dll 참조.

dll 참조 속성

1.Dll 참조 후 배포시 로컬복사 True 로 해야 해당 dll을 쓰는 프로그램이 클라이언트 PC에서 정상적으로 실행됨.

 

2.빌드시 참조된 Dll을 Dll 폴더에 넣어 관리하기.

Properties 설정

빌드전 이벤트 명령줄

if not exist "$(TargetDir)\Dll" mkdir "$(TargetDir)\Dll"

빌드 후 이벤트 명령줄

move "$(TargetDir)\*.dll" "$(TargetDir)\Dll"
del "$(TargetDir)\*.xml"
RD /S /Q "$(TargetDir)\de"
RD /S /Q "$(TargetDir)\es"
RD /S /Q "$(TargetDir)\ja"
RD /S /Q "$(TargetDir)\ru"

해당구문을 추가 후 빌드하면 exe실행파일의 경로에 Dll폴더가 없을시 폴더 생성 후 해당 폴더 안에

Dll파일을 이동시킴.

※ 위와 같이 처리시 Dll참조 경로를 app.conifg에도 참조 경로 관련 구문 추가해줘야함

app.config

SMALL

'언어 > C#' 카테고리의 다른 글

C# Build Error  (0) 2020.09.07
C# 메일전송 참조  (0) 2019.11.20
Show / ShowDialog  (0) 2019.10.01
연산자  (0) 2019.10.01
Windows Form Event 처리 순서  (0) 2019.09.30

SMALL

'언어 > C#' 카테고리의 다른 글

C# 메일전송 참조  (0) 2019.11.20
C# ] Dll 참조  (0) 2019.10.02
연산자  (0) 2019.10.01
Windows Form Event 처리 순서  (0) 2019.09.30
오류 - cannot convert DBNull into CHAR 10  (0) 2019.09.27

C# 연산자

SMALL

'언어 > C#' 카테고리의 다른 글

C# ] Dll 참조  (0) 2019.10.02
Show / ShowDialog  (0) 2019.10.01
Windows Form Event 처리 순서  (0) 2019.09.30
오류 - cannot convert DBNull into CHAR 10  (0) 2019.09.27
C# 2019/08/30 업무 History  (0) 2019.08.30

+ Recent posts